Go template.ExecuteTemplate include html

后端 未结 8 1860
我在风中等你
我在风中等你 2020-12-01 10:19

I have followed this tutorial: http://golang.org/doc/articles/wiki/final.go and have slightly modified it for my needs/wants. The problem is I would like to support HTML in

相关标签:
8条回答
  • 2020-12-01 10:55

    Here's an approach that doesn't require any changes to your existing structs, and a very minimal, additive change to your templates:

    Change these lines:

    var (
        templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
    )
    

    to this (include a funcmap with a function that will output un-escaped HTML):

    var templates = template.Must(template.New("main").Funcs(template.FuncMap{
        "safeHTML": func(b []byte) template.HTML {
            return template.HTML(b)
        },
    }).ParseFiles("tmpl/edit.html", "tmpl/view.html"))
    

    And then just change your template HTML from this:

    <div>{{printf "%s" .Body}}</div>
    

    to this (use your new function):

    <div>{{ .Body | safeHTML }}</div>
    

    Much easier!

    0 讨论(0)
  • 2020-12-01 10:57

    I created a custom function for the template as follows:

    func noescape(str string) template.HTML {
        return template.HTML(str)
    }
    
    var fn = template.FuncMap{
        "noescape": noescape,
    }
    

    Then on your template:

    {{ noescape $x.Body }}
    
    0 讨论(0)
  • 2020-12-01 11:00

    Convert your []byte or string to type template.HTML (documented here)

    p.Body = template.HTML(s) // where s is a string or []byte
    

    Then, in your template, just:

    {{.Body}}
    

    It will be printed without escaping.

    EDIT

    In order to be able to include HTML in you page's body you need to change the Page type declaration:

    type Page struct {
        Title string
        Body  template.HTML
    }
    

    then assign to it.

    0 讨论(0)
  • 2020-12-01 11:00

    In my case (where I am populating a view struct with a list of Activity), I had to change the property Message string to Message template.HTML. Then, when setting the property value I can call activity.Message = template.HTML("The <b>HTML</b>").

    0 讨论(0)
  • 2020-12-01 11:03

    For clarification and a much simpler way of passing HTML to template, see

    https://groups.google.com/forum/#!topic/golang-nuts/8L4eDkr5Q84

    Just create your HTML string via go and pass it into your template, e.g.:

    Sout := ""
    .
    .
    
        Sout += fmt.Sprintf(`<tr><td>%s<td align=center>%.2f<td>%s<td>%s<td>%s<td>%s<td align=center>%d<td align=center>%d
                        <td align=center>%d`, AccountID, amount, remissiondetails, created, begins, ends,
                        freePDFs, freeinformants, freeSDQs)
    
    .
    .
        render(w, "templates/Waivers.html", map[string]interface{}{ "Body":template.HTML(Sout), })
    
    0 讨论(0)
  • 2020-12-01 11:11

    Take a look at the template.HTML type. It can be used to encapsulate a known safe fragment of HTML (like the output from Markdown). The "html/template" package will not escape this this type.

    type Page struct {
        Title string
        Body template.HTML
    }
    
    page := &Page{
        Title: "Example",
        Body:  template.HTML(blackfriday.MarkdownCommon([]byte("foo bar")),
    }
    

    I usually write my own func Markdown(text string) html.Template method that calls blackfriday with the appropriate config and does some type conversions. Another alternative might be also to register a "html" func in the template parser, that allows you to output any value without any escaping by doing something like {{html .MySafeStr}}. The code might look like:

    var tmpl = template.Must(template.New("").Funcs(template.FuncMap{
        "html": func(value interface{}) template.HTML {
            return template.HTML(fmt.Sprint(value))
        },
    }).ParseFiles("file1.html", "file2.html"))
    
    0 讨论(0)
提交回复
热议问题