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 ofhtml/template
: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:
Output:
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 astring
url parameter as a value of typetemplate.URL
:Output:
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 asafeURL()
custom function:Output:
Try these on the Go Playground.