Handlebars 2.0运行时出现未知模板对象错误

时间:2014-09-29 18:54:54

标签: javascript exception backbone.js handlebars.js

I have published all my code as a runnable that uses express for loading static content

我已经预编译了这个把手模板:

 <img src="{{coverImage}}"/>
<ul>
    <li>{{title}}</li>
    <li>{{author}}</li>
    <li>{{releaseDate}}</li>
    <li>{{keywords}}</li>
</ul>

删除

我已经获得了这个功能:

    (function() {
  var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
templates['bookTemplate'] = template(function (Handlebars,depth0,helpers,partials,data) {
  this.compilerInfo = [4,'>= 1.0.0'];
helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
  var buffer = "", stack1, helper, functionType="function", escapeExpression=this.escapeExpression;


  buffer += "<img src=\"";
  if (helper = helpers.coverImage) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.coverImage); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "\"/>\r\n    <ul>\r\n        <li>";
  if (helper = helpers.title) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.title); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "</li>\r\n        <li>";
  if (helper = helpers.author) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.author); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "</li>\r\n        <li>";
  if (helper = helpers.releaseDate) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.releaseDate); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "</li>\r\n        <li>";
  if (helper = helpers.keywords) { stack1 = helper.call(depth0, {hash:{},data:data}); }
  else { helper = (depth0 && depth0.keywords); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; }
  buffer += escapeExpression(stack1)
    + "</li>\r\n    </ul>\r\n<button class=\"delete\">Delete</button>";
  return buffer;
  });
})();

我添加了这样的脚本:

 <script src="js/lib/handlebars.runtime-v2.0.0.js"></script>
 <script src="js/templates.js"></script>

我在Chrome Dev Tools中看到两个脚本都加载了。

然而,当我像这样使用它时:

 //inside a Backbone view
 template:Handlebars.templates.bookTemplate

我收到此错误:

 Uncaught Error: Unknown template object: function handlebars.runtime-v2.0.0.js:455template handlebars.runtime-v2.0.0.js:455hb.template handlebars.runtime-v2.0.0.js:644(anonymous function) templates.js:3(anonymous function)

我在函数handlebars runtime @2.0.0的{​​{1}}第455行收到此错误,稍作调试后我发现template是一个函数,但templateSpec是{ {1}}:

templateSpec.main

此外,我发现Handlebars.templates是一个空对象。

这里发生了什么?

2 个答案:

答案 0 :(得分:7)

如果您使用把手1.3.x预编译该模板,那么您将遇到2.0的重大变化。确保服务器/构建工具运行2.0手柄编译器。

答案 1 :(得分:0)

您确定使用与运行时相同的Handlebars版本编译模板吗?

我只是尝试Handlebars 2.0,我编译的模板在编译器字段中有[6,'>= 2.0.0']。您编译的模板表明它是使用Handlebars 1.x编译的。