【发布时间】:2014-05-01 11:58:05
【问题描述】:
我有一个模板文件 test_temp.handlebars。它的内容是,
<div>Hello {{name}}</div>
我在命令行中使用命令编译了模板,
handlebars test_temp.handlebars -f test_temp.js
test_temp.js 文件的内容是,
(function() {
var template = Handlebars.template, templates = Handlebars.templates =Handlebars.templates || {};
templates['test_temp'] = template({"compiler":[5,">=2.0.0"],"main":function(depth0,helpers,partials,data) {
var helper, functionType="function", escapeExpression=this.escapeExpression;
return "<div>Hello "
+ escapeExpression(((helper = helpers.name || (depth0 && depth0.name)),(typeof helper === functionType ? helper.call(depth0, {"name":"name","hash":{},"data":data}) : helper)))
+ "</div>\n";
},"useData":true});
})();
现在我在我的 html 中阅读我的预编译模板。
var compiledTemplate = Handlebars.templates['test_temp'];
var temp_html = compiledTemplate({ name: 'World' });
console.log(temp_html); //undefined
这里返回给 temp_html 的值是未定义的。 请让我知道如何将此 temp_html 放入 div 中。
$("#tempdiv").html(temp_html);
当我更新 div 内的 temp_html 时,抛出的错误是,
"Uncaught TypeError: undefined is not a function"
如何获取预编译的模板值并将其插入到 div 中。
【问题讨论】:
标签: javascript ember.js handlebars.js