FAQ
I asked on the IRC before coming here. I was told to ask on the mailing
list.

I am using the following template:

<?xml version="1.0" encoding="UTF-8" ?>
<metadata>
<now>
{{.CurrentTime}}
</now>
<cutoffstart></cutoffstart>
<cutoffend>
{{.CutoffTime}}
</cutoffend>
</metadata>

I am loading and calling it with the following:

// Load template.
page, err := template.ParseFiles("./xml/ordersummary.xml")
if err != nil {
return &web_error{err, "Template loading: failed to compile templates from
direcory:\n" + err.Error(), 500}
}

// Now we can execute the template.
err = page.ExecuteTemplate(w, "ordersummary.xml", content)
if err != nil {
return &web_error{err, "Template execution failed." + err.Error(), 500}
}

Expected output:

<?xml version="1.0" encoding="UTF-8" ?>
<metadata>
<now>
2012-12-16 19:52:55.708117 -1000 HST
</now>
<cutoffstart></cutoffstart>
<cutoffend>
2012-12-16 06:00:00 -1000 HST
</cutoffend>
</metadata>


Actual output:
(notice the escaped '<')

&lt;?xml version="1.0" encoding="UTF-8" ?>
<metadata>
<now>
2012-12-16 19:52:55.708117 -1000 HST
</now>
<cutoffstart></cutoffstart>
<cutoffend>
2012-12-16 06:00:00 -1000 HST
</cutoffend>
</metadata>


Why is that first '<' being escaped? I thought any content in the templates
was literally pushed out the pipe except if manipulated by {{ }} tags.

How do I prevent this behavior?

Thank you.

Jackman.

--

Search Discussions

  • Kyle Lemons at Dec 17, 2012 at 7:36 pm
    This is working as intended. The "html/template" package is for HTML, not
    XML.

    https://code.google.com/p/go/issues/detail?id=3133

    Two common workarounds:
    1) Insert it with a {{.Header}} that is typed template.HTML
    2) Write the header to the connection separately or prepend it to the
    output of the template execution.

    On Mon, Dec 17, 2012 at 1:33 AM, Jackman wrote:

    I asked on the IRC before coming here. I was told to ask on the mailing
    list.

    I am using the following template:

    <?xml version="1.0" encoding="UTF-8" ?>
    <metadata>
    <now>
    {{.CurrentTime}}
    </now>
    <cutoffstart></cutoffstart>
    <cutoffend>
    {{.CutoffTime}}
    </cutoffend>
    </metadata>

    I am loading and calling it with the following:

    // Load template.
    page, err := template.ParseFiles("./xml/ordersummary.xml")
    if err != nil {
    return &web_error{err, "Template loading: failed to compile templates from
    direcory:\n" + err.Error(), 500}
    }

    // Now we can execute the template.
    err = page.ExecuteTemplate(w, "ordersummary.xml", content)
    if err != nil {
    return &web_error{err, "Template execution failed." + err.Error(), 500}
    }

    Expected output:

    <?xml version="1.0" encoding="UTF-8" ?>
    <metadata>
    <now>
    2012-12-16 19:52:55.708117 -1000 HST
    </now>
    <cutoffstart></cutoffstart>
    <cutoffend>
    2012-12-16 06:00:00 -1000 HST
    </cutoffend>
    </metadata>


    Actual output:
    (notice the escaped '<')

    &lt;?xml version="1.0" encoding="UTF-8" ?>
    <metadata>
    <now>
    2012-12-16 19:52:55.708117 -1000 HST
    </now>
    <cutoffstart></cutoffstart>
    <cutoffend>
    2012-12-16 06:00:00 -1000 HST
    </cutoffend>
    </metadata>


    Why is that first '<' being escaped? I thought any content in the
    templates was literally pushed out the pipe except if manipulated by {{ }}
    tags.

    How do I prevent this behavior?

    Thank you.

    Jackman.

    --

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 17, '12 at 7:06p
activeDec 17, '12 at 7:36p
posts2
users2
websitegolang.org

2 users in discussion

Kyle Lemons: 1 post Jackman: 1 post

People

Translate

site design / logo © 2022 Grokbase