为什么我在Go HTML模板输出中看到ZgotmplZ?

Ran*_*tor 26 go go-templates

当我调用Go模板函数输出HTML时,它会显示ZgotmplZ.

示例代码:

http://play.golang.org/p/tfuJa_pFkm

package main

import (
    "html/template"
    "os"
)

func main() {
    funcMap := template.FuncMap{
        "printSelected": func(s string) string {
            if s == "test" {
                return `selected="selected"`
            }
            return ""
        },

        "safe": func(s string) template.HTML {
            return template.HTML(s)
        },
    }
    template.Must(template.New("Template").Funcs(funcMap).Parse(`
    <option {{ printSelected "test" }} {{ printSelected "test" | safe }} >test</option>
    `)).Execute(os.Stdout, nil)

}
Run Code Online (Sandbox Code Playgroud)

输出:

<option ZgotmplZ ZgotmplZ >test</option>
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 24

"ZgotmplZ"是一个特殊值,表示不安全内容在运行时到达CSS或URL上下文.该示例的输出将是:

 <img src="#ZgotmplZ">
Run Code Online (Sandbox Code Playgroud)

您可以向模板funcMap添加safe和attr函数:

包主

import (
    "html/template"
    "os"
)

func main() {
    funcMap := template.FuncMap{
        "attr":func(s string) template.HTMLAttr{
            return template.HTMLAttr(s)
        },
        "safe": func(s string) template.HTML {
            return template.HTML(s)
         },
    }

    template.Must(template.New("Template").Funcs(funcMap).Parse(`
    <option {{  .attr |attr }} >test</option>
        {{.html|safe}}
     `)).Execute(os.Stdout,   map[string]string{"attr":`selected="selected"`,"html":`<option selected="selected">option</option>`})
}
Run Code Online (Sandbox Code Playgroud)

输出结果如下:

<option selected="selected" >test</option>
<option selected="selected">option</option>
Run Code Online (Sandbox Code Playgroud)

您可能想要定义一些其他函数,可以将字符串转换为template.CSS,template.JS,template.JSStr,template.URL等.

  • 这也不起作用,仅供参考(对于接受输入并在输出中使用它的函数)。 (2认同)
  • 使用template.HTMLAttr而不是template.HTML,它工作正常.看到更新的代码. (2认同)

Isa*_*ten 7

package main

import (
    "html/template"
    "os"
)

type T struct {
    HTML template.HTML
    ATTR template.HTMLAttr
    URL  template.URL
    JS   template.JS
    CSS  template.CSS
}

func main() {

    data := T{
        HTML: `<div>test div</div>`,
        ATTR: `selected="selected"`,
        URL:  `https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg`,
        CSS:  `font-size: 15px`,
        JS:   `console.log("hello world")`,
    }

    template.Must(template.New("Template").Parse(`
        {{.HTML}}
        <option {{.ATTR}} style="{{.CSS}}">test</option>
        <script>{{.JS}}</script>
        <img src="{{.URL}}">
    `)).Execute(os.Stdout, data)
}
Run Code Online (Sandbox Code Playgroud)

输出

<div>test div</div>
<option selected="selected" style="font-size: 15px">test</option>
<script>console.log("hello world")</script>
<img src="https://upload.wikimedia.org/wikipedia/commons/5/53/Google_%22G%22_Logo.svg">
Run Code Online (Sandbox Code Playgroud)

游乐场示例


Ran*_*tor 5

您试图在 template/html 认为不安全的地方输出 HTML(例如,在 HTML 元素内,如下所示:

<option {{ printSelected }}>
Run Code Online (Sandbox Code Playgroud)

我找不到任何方法来说服它是安全的(包括返回 template.HTML 而不是字符串);我发现的唯一替代方法是重写模板,在本例中使用 bool 输出代替:

<option {{ if printSelected }}selected{{ end }}>
Run Code Online (Sandbox Code Playgroud)


小智 5

<img src="{{myfunction}}">在myfunction返回编码图像的地方遇到了类似的问题。

最后我解决了它,而不是返回字符串函数template.URL(mystring)