dpl9717 2016-04-08 19:30
浏览 140
已采纳

Golang。 HTML /模板。 如何将未加引号的字符串放入<script>?

I have a template:

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

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

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

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

  • 写回答

1条回答 默认 最新

  • dparivln22034 2016-04-08 21:07
    关注

    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("").Parse(`<script>{{.}}</script>` + "
    "))
    t.Execute(os.Stdout, "%7Bdata%22%3A%5B%7B%7D%7D")
    t.Execute(os.Stdout, template.JS("%7Bdata%22%3A%5B%7B%7D%7D"))
    

    Output:

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 论文AlphaTensor复现(有偿)
  • ¥15 (有偿)在ANSYS中 .anf文件
  • ¥45 关于#芯片#的问题:组合逻辑电路设计
  • ¥15 基与机器学习和时间序列分析预测养老服务需求趋势
  • ¥100 求连续两帧图像在水平和垂直上偏移
  • ¥15 mysql全文索引查找指定必须关键词word无效
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通