Handlebars预编译的模板返回未定义的值 [英] Handlebars precompiled templates returns undefined value

查看:184
本文介绍了Handlebars预编译的模板返回未定义的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板文件test_temp.handlebars。其内容是

 < div> Hello {{name}}< / div> 

我在命令行中使用命令


$编译了该模板b $ b

  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> \\\
;
},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时,抛出的错误是

 未捕获TypeError:undefined不是一个函数

如何获取预编译的模板值并将其插入到div中。

解决方案

看到这个答案:
https://stackoverflow.com/a/22214119/432089



如果您从npm下载您的手柄,您将获得2.0 alpha版本,而不是稳定的1.3.0版本。换句话说,可能你有2.0个alpha预编译模板,但是你使用的是1.3.0的运行时JS。


I have a template file test_temp.handlebars. Its content is,

<div>Hello {{name}}</div>

I compiled the template in my command line using the command,

handlebars test_temp.handlebars -f test_temp.js

The content of the test_temp.js file is,

(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});
})();

Now i read my precompiled template in my html.

var compiledTemplate = Handlebars.templates['test_temp'];
var temp_html = compiledTemplate({ name: 'World' });
console.log(temp_html);  //undefined

Here the value returned to the temp_html is undefined. Kindly let me know how to put this temp_html inside a div.

$("#tempdiv").html(temp_html);

When I update the temp_html inside the div, the error thrown is,

"Uncaught TypeError: undefined is not a function"

How to get the precompiled template value and insert it inside a div.

解决方案

See this answer: https://stackoverflow.com/a/22214119/432089

If you downloaded your handlebars from npm, you will get the 2.0 alpha build instead of the stable 1.3.0 build. In other words, it's likely you have 2.0 alpha precompiled templates, but you are using the 1.3.0 runtime JS.

这篇关于Handlebars预编译的模板返回未定义的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆