去模板.ExecuteTemplate包含html

时间:2013-08-11 19:04:43

标签: go go-templates

我遵循了本教程:http://golang.org/doc/articles/wiki/final.go并根据我的需要略微修改了它。问题是我想在模板中支持HTML。我意识到这是一个安全风险,但目前并不是一个问题。

页面渲染的结果:

<h1>this<strong>is</strong>a test</h1>

让我解释一下代码:

type Page struct {
    Title string
    Body  []byte
}

我想要的HTML数据存储在Page.Body中。这是类型[]byte,这意味着我不能(或者我可以)运行html/template.HTML(Page.Body),因为该函数需要一个字符串。

我有这个预渲染模板:

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

实际ExecuteTemplate看起来像这样:

err := templates.ExecuteTemplate(w, tmpl+".html", p)

其中w为w http.ResponseWriter,tmpl为tmpl string,p为p *Page

最后,我的'view.html'(模板)如下所示:

<h1>{{.Title}}</h1>
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
<div>{{printf "%s" .Body}}</div>

我尝试过的事情:

  • {{printf "%s" .Body | html}}没有做任何事情
  • 我已添加github.com/russross/blackfriday(Markdown处理器)并运行p.Body = blackfriday.MarkdownCommon(p.Body),正确将Markdown转换为HTML,但HTML仍然作为实体输出。
  • 编辑:我尝试了以下一些代码(我不知道为什么格式搞砸了),它仍然输出完全相同的。

    var s template.HTML s = template.HTML(p.Body) p.Body = []byte(s)

非常感谢任何指导。如果我感到困惑,请问我可以修改我的问题。

8 个答案:

答案 0 :(得分:47)

将您的[]bytestring转换为template.HTML类型(已记录为here

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

然后,在您的模板中,只需:

{{.Body}}

它将被打印而不会逃脱。

修改

为了能够在您网页的正文中包含HTML,您需要更改Page类型声明:

type Page struct {
    Title string
    Body  template.HTML
}

然后分配给它。

答案 1 :(得分:16)

查看template.HTML类型。它可以用于封装HTML的已知安全片段(如Markdown的输出)。 “html / template”包不会逃避此类型。

type Page struct {
    Title string
    Body template.HTML
}

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

我通常编写自己的func Markdown(text string) html.Template方法,使用适当的配置调用blackfriday并执行某些类型的转换。另一种替代方法可能是在模板解析器中注册一个“html”函数,它允许您通过执行{{html .MySafeStr}}之类的操作来输出任何值而不进行任何转义。代码可能如下所示:

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"))

答案 2 :(得分:7)

我为模板创建了一个自定义函数,如下所示:

func noescape(str string) template.HTML {
    return template.HTML(str)
}

var fn = template.FuncMap{
    "noescape": noescape,
}

然后在你的模板上:

{{ noescape $x.Body }}

答案 3 :(得分:1)

我正在使用Beego和React.js,并试图让JSX解析器运行数小时。结果是html / template剥离注释特别是js doc block / ** @jsx React.DOM * /。

通过创建一个特殊方法来解决它,将注释键入为JS并从模板中调用它。

// Create a method in your controller (I'm using Beego)
func jsxdoc()(out template.JS) {
    return template.JS(`/** @jsx React.DOM */`)
}

// Add method to your function map available to views
beego.AddFuncMap("jsxdoc", jsxdoc)

// In template
<script type="text/jsx">
    {{ jsxdoc }}
    var CommentBox = React.createClass({
      render: function() {
        return (
          <div class="commentBox">
            Hello, world! I am a CommentBox.
          </div>
        );
      }
    });
    React.renderComponent(
      <CommentBox />,
      document.getElementById('content')
    );
</script>

答案 4 :(得分:1)

这是一种不需要对现有结构进行任何更改的方法,也是对模板进行非常小的附加更改的方法:

更改这些行:

composer require illuminate/notifications:~5.3

到此(包含一个带有将输出未转义HTML的函数的funcmap):

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

然后只需更改模板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"))

到此(使用你的新功能):

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

更容易!

答案 5 :(得分:0)

有关澄清以及将HTML传递给模板的更简单方法,请参阅

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

只需通过go创建HTML字符串并将其传递到模板中,例如:

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), })

答案 6 :(得分:0)

在我的情况下(我在视图struct中填充了Activity的列表),我不得不将属性Message string更改为Message template.HTML。然后,在设置属性值时,我可以调用activity.Message = template.HTML("The <b>HTML</b>")

答案 7 :(得分:-1)

为什么不将[]byte转换为字符串?你可以这样做:

str := string(page.Body)