レンダリングされない Underscore.js のテンプレートがあります。私の問題はこの JSFiddleで説明されています。
// this string will get loaded by an ajax call and put in a variable when the application starts
var allTemplates = '<section id="header">header template</section> <section id="content"><% var test = 10 %><%= test %></section>"';
// put it in the DOM to parse it with jQuery
$('#test').html(allTemplates);
var getSpecificTemplate = function(templateID) {
return $('#test').find('#' + templateID).html();
};
var templateData = getSpecificTemplate('content');
// expected log output: 10
// actual log output: <% var test = 10 %><%= test %>
console.log( _.template(templateData, {}) );
// why?
このセットアップは、私のコードとほとんど同じです。何が問題なのですか?テンプレートがエンコードされているのはなぜですか?