我使用下划线+express来渲染网页,这是一个模板示例:
<!doctype>
<html>
<head>
<meta charset="utf-8">
<title><%= title %></title>
</head>
<body>
<div class="description"><%= description %></div>
</body>
</html>
到目前为止一切都很好,但是我想在页面中使用以下标记:
<script type="text/template">
<div class="foo"><%= bar %></div>
</script>
如果我使用它,那么服务器端下划线将尝试解析它,但这不是我的意图,我也想在客户端使用 _.template 。
有什么方法可以忽略模板的某些部分吗?我浏览了文档,但没有找到任何东西。像这样的东西真的很有用:
<%! %>
示例:
<%!
<script type="text/template">
<%= text %>
</script>
%>
我知道我可以部分编译页面,但是有更好的解决方案吗?
最佳答案
您可以将客户端模板设置设置为使用不同的插值设置,这样它们就不会与服务器端的设置冲突。请参阅http://underscorejs.org/#template
_.templateSettings = {
interpolate: /<%!(.+?)%>/g
};
请注意,您可能必须转义某些字符。
关于javascript - 在另一个模板中为模板 <script type="text/template></script> 添加下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22254080/