避免在html模板中自动转义 [英] avoid auto escape in html template

查看:736
本文介绍了避免在html模板中自动转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用URL呈现HTML模板。



我尝试使用 template.URL(http: //myurl.com/(data)/aaa.jpg)还有 template.HTML(http://myurl.com/(data)/aaa.jpg )但它仍然会转义括号。



我使用 gin gonic

  router.GET(/测试,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),
$ b

模板文件:

 < div> 
< img src ={{。url}}/>
< / div>

最后输出:

 < ; div> 
< img src =http://myurl.com/%28data%29/aaa.jpg/>
< / div>


解决方案

使用 template.URL 就足够了。



您看到不是给定URL的HTML编码,您看到的是(data)部分中的开始和结束括号的URL编码。那很好。 %28 '('(28是开头括号字符的六进制代码)的编码, code>%29 是')'字符。



url http://myurl.com/(data)/aaa.jpg http://myurl.com/%28data%29/aaa .jpg 是一样的。



URL编码的目的是使URL中包含的URL和值(例如表单参数)


网址编码

URL只能使用ASCII字符集通过Internet发送。



由于URL通常包含ASCII集外的字符,因此必须将URL转换为有效ASCII格式。



网址编码会将不安全的ASCII字符替换为后跟两个十六进制数字的%。
网址不能包含空格。 URL编码通常会用加号(+)或%20替换空格。

您可以在此处阅读有关URL编码的更多信息: HTML网址编码参考



测试它:

  func rh(w http.ResponseWriter,r * http.Request){
fmt.Fprint (w,`< html>< body>
< img src =http:// localhost:8080 /(data)/aaa.jpg>
< img src = http:// localhost:8080 /%28data%29 / aaa.jpg>
< / body>< / htm>`)
}

func (w http.ResponseWriter,r * http.Request){
http.ServeFile(w,r,aaa.jpg)
}

func main(){
http.HandleFunc(/,rh)
http.HandleFunc(/(data)/aaa.jpg,imgh)
panic(http.ListenAndServe(localhost:8080, nil))
}

现在将您的浏览器指向 http: //本地主机:8080 。它会显示2张图片。


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 and http://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.

URL Encoding

URLs can only be sent over the Internet using the ASCII character-set.

Since URLs often contain characters outside the ASCII set, the URL has to be converted into a valid ASCII format.

URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

You can read more about URL encoding here: HTML URL Encoding Reference.

Testing it:

func rh(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, `<html><body>
<img src="http://localhost:8080/(data)/aaa.jpg">
<img src="http://localhost:8080/%28data%29/aaa.jpg">
</body></htm>`)
}

func imgh(w http.ResponseWriter, r *http.Request) {
    http.ServeFile(w, r, "aaa.jpg")
}

func main() {
    http.HandleFunc("/", rh)
    http.HandleFunc("/(data)/aaa.jpg", imgh)
    panic(http.ListenAndServe("localhost:8080", nil))
}

Now direct your browser to http://localhost:8080. It will display 2 images.

这篇关于避免在html模板中自动转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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