如何摆脱Golang中的html / template中的ZgotmplZ? [英] How to get rid of ZgotmplZ from html/template in Golang?

查看:590
本文介绍了如何摆脱Golang中的html / template中的ZgotmplZ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在后端使用Golang。当我使用html / templates呈现html时,我得到了URL的 ZgotmplZ

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

我在服务器端使用GitURL的字符串。这个URL是 https 。当我寻找解决方案时,一些博客建议使用 safeURL 。所以我试过,

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

但是代码没有编译。

有人可以帮我解决这个问题吗?任何建议都会非常有用。

表明你的输入无效。从 html / template 文档引用:


 ZgotmplZ是一个特殊值,表示不安全内容在运行时到达
CSS或URL上下文。示例的输出将是
< img src =#ZgotmplZ>
如果数据来自受信任的来源,请使用内容类型从过滤中免除
:URL(`javascript:...`)。


如果您要替换有效 url文本,没有什么特别的,比如像 safeURL 函数是需要的。如果您的模板执行结果为像#ZgotmplZ这样的值,则表示您要插入的网址无效。



看到这个例子:

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

输出:

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

您可以使用 template.URL ,如果您想按原样使用URL而不会转义。请注意,在这种情况下,提供的值将被原样使用,即使它不是有效的URL。



safeURL 不是某种可用于模板的魔术或预定义函数。但是你可以注册你自己的自定义函数,它返回字符串 url参数作为类型 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)

输出:

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

注意:如果您能够传入 template.URL 直接赋值给模板执行,你不需要注册并使用 safeURL()自定义函数:

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

输出:

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

试试这些 Go Goground


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.

解决方案

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>` + "\n"))
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>` + "\n"))
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>` + "\n"))
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.

这篇关于如何摆脱Golang中的html / template中的ZgotmplZ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆