Trying to render HTML template with an URL. The problem is that the URL contains () in it, and those characters are escaped.
I tried to use template.URL("http://myurl.com/(data)/aaa.jpg")
and also template.HTML("http://myurl.com/(data)/aaa.jpg")
but it still escape brackets.
I'm using gin gonic.
router.GET("/test", func(c *gin.Context) {
c.HTML(http.StatusOK, "test.tmpl", gin.H{
"url": template.URL("http://myurl.com/(data)/aaa.jpg"),
// "url": template.HTML("http://myurl.com/(data)/aaa.jpg"),
})
Template file :
<div>
<img src="{{.url}}" />
</div>
final ouput :
<div>
<img src="http://myurl.com/%28data%29/aaa.jpg"/>
</div>
Using a value of
template.URL
is perfectly enough.What you see is not HTML encoding of the given URL, what you see is the URL encoding of the opening and closing parenthesis in the
(data)
part. That is perfectly ok.%28
is the encoding of'('
(28 is the hexa code for the opening parenthesis character), and%29
is the')'
character.The url
http://myurl.com/(data)/aaa.jpg
andhttp://myurl.com/%28data%29/aaa.jpg
are one and the same.The purpose of the URL encoding is so that URLs and values included in URLs (e.g. form parameters) can be safely transmitted.
You can read more about URL encoding here: HTML URL Encoding Reference.
Testing it:
Now direct your browser to
http://localhost:8080
. It will display 2 images.