高朗html/模板.如何将未加引号的字符串放在< script>中? [英] Golang. html/template. How to put a unquoted string to <script>?

查看:155
本文介绍了高朗html/模板.如何将未加引号的字符串放在< script>中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板:

<script type="text/template" id="data-user">{{.User}}</script>

其中用户"是URL编码格式的json字符串.像

Where "User" is json string in URL encoding format. Something like

%7Bdata%22%3A%5B%7B%7D%7D

但是默认的 html/template 将其放在引号内

But default html/template put it inside quotes like

"%7Bdata%22%3A%5B%7B%7D%7D"

我尝试了 html/template godoc参考中的内容 Context {{.}} After {{.}} O'Reilly: How are &lt;i&gt;you&lt;/i&gt;? <a title='{{.}}'> O&#39;Reilly: How are you? <a href="/{{.}}"> O&#39;Reilly: How are %3ci%3eyou%3c/i%3e? <a href="?q={{.}}"> O&#39;Reilly%3a%20How%20are%3ci%3e...%3f <a onx='f("{{.}}")'> O\x27Reilly: How are \x3ci\x3eyou...? <a onx='f({{.}})'> "O\x27Reilly: How are \x3ci\x3eyou...?" <a onx='pattern = /{{.}}/;'> O\x27Reilly: How are \x3ci\x3eyou...\x3f

I tried that stuff from html/template godoc reference Context {{.}} After {{.}} O'Reilly: How are &lt;i&gt;you&lt;/i&gt;? <a title='{{.}}'> O&#39;Reilly: How are you? <a href="/{{.}}"> O&#39;Reilly: How are %3ci%3eyou%3c/i%3e? <a href="?q={{.}}"> O&#39;Reilly%3a%20How%20are%3ci%3e...%3f <a onx='f("{{.}}")'> O\x27Reilly: How are \x3ci\x3eyou...? <a onx='f({{.}})'> "O\x27Reilly: How are \x3ci\x3eyou...?" <a onx='pattern = /{{.}}/;'> O\x27Reilly: How are \x3ci\x3eyou...\x3f

但是我还没有成功. 感谢您的帮助

But I haven't had success. Appreciate your help

推荐答案

谢谢!我找到了解决方案.有 template.JS 类型. 我将字符串转换为template.JS,并且可以正常工作.

Thank you! I found solution. There are template.JS type. I converted string to template.JS and it works.

请参见以下示例:

t := template.Must(template.New("").Parse(`<script>{{.}}</script>` + "\n"))
t.Execute(os.Stdout, "%7Bdata%22%3A%5B%7B%7D%7D")
t.Execute(os.Stdout, template.JS("%7Bdata%22%3A%5B%7B%7D%7D"))

输出:

<script>"%7Bdata%22%3A%5B%7B%7D%7D"</script>
<script>%7Bdata%22%3A%5B%7B%7D%7D</script>

转到游乐场 中尝试这些.

Try these on the Go Playground.

这篇关于高朗html/模板.如何将未加引号的字符串放在&lt; script&gt;中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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