【发布时间】:2012-06-23 15:23:20
【问题描述】:
如何在underscore.js 模板中为使用backbone.js 构建的应用设置变量?我只想创建可重用的处理字符串。还有,underscore.js的内置函数_.escape可以用来处理这些变量吗?
<script type="text/html" id="templateresults">
<p><%= encodeURIComponent(title) %></p> // this works
// try 1:
var encodedTitle = encodeURIComponent(title); // shows up as regular text
<p>'+encodedTitle+'</p> // this doesn't work and shows up as regular text
// try 2:
<% var encodedTitle = encodeURIComponent(title); %> // nothing shows up
<p><% '+encodedTitle+' %></p> // nothing shows up
</script>
title 是一个 JSON 项(文本字符串)。
编码输出:This%20is%20a%20Sample%20Title
常规输出:This is a Sample Title
【问题讨论】:
标签: javascript underscore.js template-engine