【问题标题】:Prevent Jade interpolation for script template脚本模板防止 Jade 插值
【发布时间】:2012-11-21 21:15:29
【问题描述】:

我正在使用 node.js 和 express.js 来呈现一个 index.jade 页面,该页面包含几个脚本块,这些脚本块包含要通过主干和下划线使用的模板。我面临的问题是,由于模板中包含 样式变量,Jade 渲染失败。以下代码 sn -p 导致语法错误:

script#tpl-things-list-item(type='text/template')
  td 
    a(href=<%= _id %>) link text
  td <%= name %>
  td <%= age %>

请注意,只有当我在 href 值中使用变量时才会出现问题,如果我删除整个 href,这个 sn-p 就可以正常工作。有没有办法解决这个问题?我想继续使用 Jade 来定义模板,因为它非常简洁,但这是一个阻碍。

【问题讨论】:

  • 我也想不通。奇怪。

标签: node.js backbone.js underscore.js pug


【解决方案1】:

知道了。

!!! 5
html(lang='en')
    head
        title= title
    body
        h1= "Hello World!"
        script#tpl-things-list-item(type='text/template')
            td
                a(href!="<%= _id %>") link text
            td <%= name %>
            td <%= age %>

【讨论】:

  • “!”是什么意思?在 href 调用中的“=”之前做什么?
  • 它生成“未转义的可执行代码”github.com/visionmedia/jade#readme
  • 对我来说,这会打印文字字符串,例如a href!="&lt;%= _id %&gt;" 而不是 a href="123"。有什么建议么?我的完整脚本是:script(type='text/template') a(href!="&lt;%= _id%&gt;")&lt;%= _id%&gt;
  • 我通过使用 | 找到了解决方法但这似乎并不干净:/例如|&lt;a href=&lt;%= _id %&gt;&gt; &lt;%= _id%&gt;&lt;/a&gt;
  • 似乎 just 在 href 值周围的引号就足够了,因为客户端模板不是“未转义的可执行代码”。我已经成功地在翡翠模板中使用了 angular.js 插值(href="{{ url }}")。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 1970-01-01
  • 2015-03-14
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
相关资源
最近更新 更多