Cách thiết lập biến bên trong mẫu underscore.js
cho ứng dụng được tạo bằng backbone.js
? Tôi chỉ muốn tạo các chuỗi đã xử lý có thể tái sử dụng. Ngoài ra, có thể sử dụng các chức năng tích hợp của underscore.js
như _.escape
để xử lý các biến đó?biến bên trong mẫu 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
là một mục JSON (chuỗi văn bản).
Encoded đầu ra: This%20is%20a%20Sample%20Title
Regular đầu ra: This is a Sample Title
Ngọt ngào! Nó hoạt động. Cảm ơn! – Steve