如何让Jade停止HTML编码元素属性,并生成一个文字字符串值?

Der*_*ley 37 templates html-encode string-literals underscore.js pug

UPDATE Jade v0.24.0使用!=属性语法修复此问题.option(value!='<%= id %>')


我正在尝试<option>使用jade 构建一个,其中选项的值是UnderscoreJS模板标记:<%= id %>但我无法让它工作,因为jade将我的标记文本转换为&lt;= id &gt;.

这是我的Jade标记:

script(id="my-template", type="text/template")
  select(id="type")
    &lt;% _.each(deviceTypes, function(type){ %>
    option(value='&lt;%= type.id %>') <%= type.name %>
    &lt;% }) %>
Run Code Online (Sandbox Code Playgroud)

我希望它能产生这个html:

<script id="my-template" type="text/template">
  <select id='type'>
    <% _.each(deviceTypes, function(type){ %>
    <option value="<%= type.id %>"> <%= type.name %> </option>
    <% }) %>
  </select>
</script>
Run Code Online (Sandbox Code Playgroud)

但我得到的是这样的:

<script id="my-template" type="text/template">
  <select id='type'>
    <% _.each(deviceTypes, function(type){ %>
    <option value="&lt;%= type.id %&gt;"> <%= type.name %> </option>
    <% }) %>
  </select>
</script>
Run Code Online (Sandbox Code Playgroud)

注意<option>输出行中非常细微的差别...... value选项的属性是HTML编码的.

如何防止Jade对此值进行HTML编码?我需要它来生成文字值,就像它对选项文本一样.

Min*_*ime 100

Derick已经提到Jade在更新中为unescape HTML编码添加了新功能,但我想为可能无法识别的人添加一些附录.

- var html = "<script></script>"
| !{html} <-- Escaped
| #{html} <-- Encoded
Run Code Online (Sandbox Code Playgroud)

来自https://github.com/visionmedia/jade


Mik*_*Lin 34

此功能已添加到Jade.!=如果要取消属性值,只需使用运算符:

script#my-template(type='text/template')
  a(href!='<%= url =>') Clicky clicky...
Run Code Online (Sandbox Code Playgroud)

  • 我知道 - [我修好了](https://github.com/jadejs/jade/issues/1087#issuecomment-20779899);) (2认同)

Kev*_*nte 5

在撰写本文时,我不相信有办法解决这个问题.请参阅此问题:https: //github.com/visionmedia/jade/issues/198

我最终放入原始HTML来解决它,使用| 字首.

  • 谢谢,凯文.回到| 让它现在起作用......生活在前沿......:P (2认同)