【问题标题】:Why am I seeing ZgotmplZ in my Go HTML template output?为什么我在 Go HTML 模板输出中看到 ZgotmplZ?
【发布时间】:2013-01-23 19:05:22
【问题描述】:

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


    【解决方案1】:

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

     <img src="#ZgotmplZ">
    

    您可以在模板funcMap中添加一个安全和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>`})
    }
    

    输出将如下所示:

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

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

    【讨论】:

    • 这也不起作用,仅供参考(对于接受输入并在输出中使用它的函数)。
    • safe 可以让你在模板中显示原始 html。
    • 使用 template.HTMLAttr 而不是 template.HTML,它工作正常。查看更新的代码。
    • 这行得通,谢谢。为后代演示:play.golang.org/p/nq3-7GYAoY
    • 函数迭代是不必要的
    【解决方案2】:

    我在&lt;img src="{{myfunction}}"&gt; 遇到了类似的问题,其中 myfunction 返回编码图像。

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

    【讨论】:

      【解决方案3】:

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

      <option {{ printSelected }}>
      

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

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

      【讨论】:

      • 就个人而言,我更喜欢你的建议,原因很简单:尽管有所有其他正确答案,但你的答案——虽然可能“更冗长”——是唯一保证不安全的内容将永远到达模板。恕我直言,这应该是最好的做事方式。任何依赖“愚弄”Go 来接受潜在不安全代码的技巧或黑客都是……嗯,不安全,是吗?
      【解决方案4】:

      最简单的方法:

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

      【讨论】:

      • 如果您想将文本渲染到 href 属性中,这是正确的答案,例如:&lt;a href="{{.Host}}"&gt;Hi&lt;/a&gt;,Host 应该是 template.URL 以避免 ZgotmplZ
      【解决方案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">
      

      playground Example

      【讨论】:

        【解决方案6】:

        您应该将字符串包装在HTMLAttr 中,该HTMLAttr 是为插入尖括号之间的文本而设计的。根据文档:

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

        HTMLAttr 封装来自可信来源的 HTML 属性,例如, dir="ltr"

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

        type HTMLAttr string

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-01
          • 1970-01-01
          • 2012-03-19
          • 2019-12-30
          • 2019-05-18
          • 1970-01-01
          相关资源
          最近更新 更多