14
如何在backbone.js
構建的應用程序的underscore.js
模板中設置變量?我只想創建可重用的處理過的字符串。另外,可以使用underscore.js
的內置函數(如_.escape
)來處理這些變量嗎?下面的變量underscore.js模板
<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
甜!有用。謝謝! – Steve