donglanche9257 2016-04-03 07:14
浏览 170

如何从Golang中的html / template摆脱ZgotmplZ?

I'm using Golang in backend. When I render the html using html/templates I'm getting ZgotmplZ for URL's.

{{if .UserData.GitURL}}
<li>
  <a href="{{.UserData.GitURL}}">
    <i class="icon fa fa-github"></i>
  </a>
</li>
{{end}}

I'm using string for GitURL in server side. This URL is https. When I looked for solutions some blog suggested to use safeURL. So I tried,

{{if .UserData.GitURL}}
<li>
  <a href="{{.UserData.GitURL | safeURL}}">
    <i class="icon fa fa-github"></i>
  </a>
</li>
{{end}}

But code didn't compile.

Could someone help me with this? Any suggestion would be really helpful.

  • 写回答

1条回答 默认 最新

  • dongtuoleng8624 2016-04-04 07:14
    关注

    ZgotmplZ is a special value indicating your input was invalid. Quoting from the doc of html/template:

    "ZgotmplZ" is a special value that indicates that unsafe content reached a
    CSS or URL context at runtime. The output of the example will be
       <img src="#ZgotmplZ">
    If the data comes from a trusted source, use content types to exempt it
    from filtering: URL(`javascript:...`).
    

    If you want to substitute a valid url text, nothing special like like safeURL function is needed. If your template execution results in a value like "#ZgotmplZ", that means the URL you wanted to insert is invalid.

    See this example:

    t := template.Must(template.New("").Parse(`<a href="{{.}}"></a>` + "
    "))
    t.Execute(os.Stdout, "http://google.com")
    t.Execute(os.Stdout, "badhttp://google.com")
    

    Output:

    <a href="http://google.com"></a>
    <a href="#ZgotmplZ"></a>
    

    You may use a value of type template.URL if you want to use a URL as-is without escaping. Note that in this case the provided value will be used as-is even if it is not a valid URL.

    safeURL is not some kind of magic or predeclared function that you may use in templates. But you may register your own custom function which returns a string url parameter as a value of type template.URL:

    t2 := template.Must(template.New("").Funcs(template.FuncMap{
        "safeURL": func(u string) template.URL { return template.URL(u) },
    }).Parse(`<a href="{{. | safeURL}}"></a>` + "
    "))
    t2.Execute(os.Stdout, "http://google.com")
    t2.Execute(os.Stdout, "badhttp://google.com")
    

    Output:

    <a href="http://google.com"></a>
    <a href="badhttp://google.com"></a>
    

    Note: If you are able to pass in a template.URL value directly to the template execution, you do not need to register and use a safeURL() custom function:

    t3 := template.Must(template.New("").Parse(`<a href="{{.}}"></a>` + "
    "))
    t3.Execute(os.Stdout, template.URL("http://google.com"))
    t3.Execute(os.Stdout, template.URL("badhttp://google.com"))
    

    Output:

    <a href="http://google.com"></a>
    <a href="badhttp://google.com"></a>
    

    Try these on the Go Playground.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题