underscore.js
如何在模板中为使用构建的应用程序设置变量backbone.js
?我只想创建可重用的处理字符串。另外,可以使用underscore.js
's 的内置函数_.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