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

问题描述 投票:0回答:7

当我调用 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)

}

输出:

<option ZgotmplZ ZgotmplZ >test</option>
go go-templates
7个回答
40
投票

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

<img src="#ZgotmplZ">

您可以在模板中添加 safe 和 attr 功能

funcMap
:

package main

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

输出将如下所示:

<option selected="selected">test</option>
<option selected="selected">option</option>

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


11
投票

我也遇到了类似的问题

<img src="{{myfunction}}">
,其中 myfunction 返回编码图像。

最后我解决了这个问题,而不是字符串函数返回

template.URL(mystring)


5
投票

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

<option {{ printSelected }}>

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

<option {{ if printSelected }}selected{{ end }}>

5
投票
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)
}

输出

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

游乐场示例


3
投票

最简单的方法:

import "html/template"
yourhref = template.URL(yourhref)

2
投票

您应该将字符串包裹在

HTMLAttr
中,它是为在尖括号之间注入的文本而设计的。根据文档:

https://golang.org/pkg/html/template/#HTMLAttr

HTMLAttr 封装来自可信来源的 HTML 属性,例如,

 dir="ltr"

使用这种类型会带来安全风险:封装的内容应来自受信任的来源,因为它将逐字包含在模板输出中。

type HTMLAttr string


0
投票

我试图将 frontmatter 中的图像插入到我的模板中,但不断收到相同的错误。我这样解决了:

{{ if isset .Params "image" }}
    {{ $myVar := print .Params.image }}
    <img src="{{ $myVar }}">
{{ end }}

请注意,我首先将

.Params.image
保存为变量,然后将其插入为我的
src

© www.soinside.com 2019 - 2024. All rights reserved.