Golang. html/template. How to put a unquoted string to <script>?

huangapple go评论85阅读模式
英文:

Golang. html/template. How to put a unquoted string to <script>?

问题

我有一个模板:

&lt;script type=&quot;text/template&quot; id=&quot;data-user&quot;&gt;{{.User}}&lt;/script&gt;

其中"User"是以URL编码格式的JSON字符串。类似于

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

但是默认的html/template会将其放在引号中,如下所示

&quot;%7Bdata%22%3A%5B%7B%7D%7D&quot;

我尝试了html/template godoc参考文档中的方法

Context {{.}} After {{.}} O&#39;Reilly: How are &amp;lt;i&amp;gt;you&amp;lt;/i&amp;gt;? &lt;a title=&#39;{{.}}&#39;&gt; O&amp;#39;Reilly: How are you? &lt;a href=&quot;/{{.}}&quot;&gt; O&amp;#39;Reilly: How are %3ci%3eyou%3c/i%3e? &lt;a href=&quot;?q={{.}}&quot;&gt; O&amp;#39;Reilly%3a%20How%20are%3ci%3e...%3f &lt;a onx=&#39;f(&quot;{{.}}&quot;)&#39;&gt; O\x27Reilly: How are \x3ci\x3eyou...? &lt;a onx=&#39;f({{.}})&#39;&gt; &quot;O\x27Reilly: How are \x3ci\x3eyou...?&quot; &lt;a onx=&#39;pattern = /{{.}}/;&#39;&gt; O\x27Reilly: How are \x3ci\x3eyou...\x3f

但是我没有成功。感谢你的帮助。

英文:

I have a template:

&lt;script type=&quot;text/template&quot; id=&quot;data-user&quot;&gt;{{.User}}&lt;/script&gt;

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

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

But default html/template put it inside quotes like

&quot;%7Bdata%22%3A%5B%7B%7D%7D&quot;

I tried that stuff from html/template godoc reference

Context {{.}} After
{{.}} O&#39;Reilly: How are &amp;lt;i&amp;gt;you&amp;lt;/i&amp;gt;?
&lt;a title=&#39;{{.}}&#39;&gt; O&amp;#39;Reilly: How are you?
&lt;a href=&quot;/{{.}}&quot;&gt; O&amp;#39;Reilly: How are %3ci%3eyou%3c/i%3e?
&lt;a href=&quot;?q={{.}}&quot;&gt; O&amp;#39;Reilly%3a%20How%20are%3ci%3e...%3f
&lt;a onx=&#39;f(&quot;{{.}}&quot;)&#39;&gt; O\x27Reilly: How are \x3ci\x3eyou...?
&lt;a onx=&#39;f({{.}})&#39;&gt; &quot;O\x27Reilly: How are \x3ci\x3eyou...?&quot;
&lt;a onx=&#39;pattern = /{{.}}/;&#39;&gt; O\x27Reilly: How are \x3ci\x3eyou...\x3f

But I haven't had success.
Appreciate your help

答案1

得分: 3

谢谢!我找到了解决方案。有一个名为template.JS的类型。
我将字符串转换为template.JS,它可以正常工作。

看看这个例子:

t := template.Must(template.New("").Parse(`<script>{{.}}</script>` + "\n"))
t.Execute(os.Stdout, `{"data":[{"}}`)
t.Execute(os.Stdout, template.JS(`{"data":[{"}}`))

输出:

<script>"{"data":[{"}}"</script>
<script>{"data":[{"}}<script>

在<kbd>Go Playground</kbd>上尝试一下。

英文:

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

See this example:

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

Output:

&lt;script&gt;&quot;%7Bdata%22%3A%5B%7B%7D%7D&quot;&lt;/script&gt;
&lt;script&gt;%7Bdata%22%3A%5B%7B%7D%7D&lt;/script&gt;

Try these on the <kbd>Go Playground</kbd>.

huangapple
  • 本文由 发表于 2016年4月9日 03:30:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/36507836.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定