我只是想知道如何在 .aspx View 中使用下划线模板,因为下划线使用的 <%= %> 标记会被 .aspx 渲染引擎拾取并给我错误。
例如:
<script type="text/template" id="my-template">
<span class="event" title="<%= description %>">
<%= title %>
</span>
</script>
这个模板给了我一个错误,因为 .aspx 渲染引擎认为我正在尝试将这些东西绑定(bind)到模型。
谢谢。
最佳答案
来自fine manual :
template
_.template(templateString, [data], [settings])
[...]
If ERB-style delimiters aren't your cup of tea, you can change Underscore's template settings to use different symbols to set off interpolated code. Define an interpolate regex to match expressions that should be interpolated verbatim, an escape regex to match expressions that should be inserted after being HTML escaped, and an evaluate regex to match expressions that should be evaluated without insertion into the resulting string.
所以如果默认<%=...%>
, <%-...%>
,和<%...%>
分隔符不适合您,那么您可以通过简单的配置更改来使用不同的分隔符。例如,如果您想使用 {%...%}
而不是<%...%>
,然后在 underscore.js
之后执行此操作已加载并在您使用 _.template
之前:
_.templateSettings = {
interpolate: /\{%=(.+?)%\}/g,
escape: /\{%-(.+?)%\}/g,
evaluate: /\{%(.+?)%\}/g
};
关于asp.net-mvc - 如何在 ASP.Net MVC View 中使用下划线/javascript 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16016316/