TypeError将预编译的Handlebars模板与上下文一起应用

ele*_*ele 21 javascript node.js handlebars.js

请原谅noob问题但是我根本无法获得预编译的Handlebars模板来做任何事情,除了barf out

TypeError:'undefined'不是函数(评估'templateSpec.call(container,Handlebars,context,options.helpers,options.partials,options.data)')

每次我将上下文应用于(预编译)模板.

鉴于以下文件和内容:

  • hello.handlebars:<p>Hello, {{name}}</p>
  • templates.js:编译的结果hello.handlebars通过handlebars hello.handlebars -f templates.js
  • index.html的:包括把手RC1运行中head,这对身体:

    <body id="body">
      <script src="templates.js" type="text/javascript" charset="utf-8"></script>
      <script type="text/javascript" charset="utf-8">
        var compiledTemplate = Handlebars.templates['hello'];
        var html = compiledTemplate({ name: 'World' });
        document.getElementById('body').innerHTML = html;
      </script>
    </body>
    
    Run Code Online (Sandbox Code Playgroud)

compiledTemplate()无论我做什么,都会调用上面的错误 - 但我能够让客户端模板编译并显示得很好.我见过的所有演练和教程都很明显,所以我必须错过一些愚蠢的东西.有任何想法吗?

bio*_*oth 27

请务必匹配Handlebars服务器和客户端软件包的版本.检查已编译模板的版本的方法如下:

把手a_template_of_yours.handlebars | grep"compilerInfo"

它应该输出这样的东西:

this.compilerInfo = [4,'> = 1.0.0'];

第一个值是Compiler Revision,它应该与浏览器中以下代码的输出相匹配:

Handlebars.COMPILER_REVISION

如果您使用npm下载Handlebars编译器,请指定所需的版本.以下是handlebars.runtime-v1.3.0.js的示例:

npm install handlebars@1.3.0 -g

  • 这对我有帮助.npm默认安装`handlebars-2.0.0-alpha` ...与运行时1.3.0不兼容 (7认同)