【问题标题】:TypeError applying precompiled Handlebars templates with a context应用带有上下文的预编译 Handlebars 模板的 TypeError
【发布时间】:2014-03-24 17:08:45
【问题描述】:

请原谅这个菜鸟问题,但我根本无法获得预编译的 Handlebars 模板来做任何事情,除了吐出来

TypeError: 'undefined' is not a function(评估'templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data)')

每次我将上下文应用到(预编译的)模板时。

给定以下文件和内容:

  • hello.handlebars<p>Hello, {{name}}</p>
  • templates.js:通过handlebars hello.handlebars -f templates.js编译hello.handlebars的结果
  • index.html:在 head 中包含 Handlebars RC1 runtime,而这用于正文:

    <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>
    

无论我做什么,调用compiledTemplate() 都会抛出该错误——但我能够让客户端模板编译并显示得很好。我看到的所有演练和教程都跳过了这一点,所以我一定错过了一些愚蠢的东西。有什么想法吗?

【问题讨论】:

    标签: javascript node.js handlebars.js


    【解决方案1】:

    我的问题是目前(2014 年 6 月),GitHub 上的 2.0 版本标记为 alpha https://github.com/wycats/handlebars.js/releases

    如果我通过bower 安装车把,它会选择最新的稳定版本。 v1.3.0.

    但是,npm install handlebars 会安装 ~2.0.0-alpha.4 版本。

    解决方法:bower.json中的版本更新为~2.0.*,然后运行bower update

    【讨论】:

      【解决方案2】:

      确保与 Handlebars 服务器和客户端软件包的版本相匹配。检查编译模板版本的方法如下:

      车把 a_template_of_yours.handlebars | grep "编译器信息"

      它应该输出如下内容:

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

      第一个值是编译器版本,它应该与浏览器上以下代码的输出相匹配:

      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 不兼容
      • 在这两种情况下我都在运行the 1.0.rc.1
      • @ele,如果可能的话,我建议您升级到更新版本的 Handlebars,因为 1.0.rc.1 的日期是 2012 年 9 月。最新的稳定版本是 1.3.0 的 1 月2014 年,因此您可能还会遗漏很多改进。您可以从Handlebars Home 下载最新的稳定客户端(即运行时 js 模块)。节点编译器(服务器模块)可以从 npm 下载(指定版本,以防止下载一些新的 alpha 版本),或者从Github releases site
      • handlebars &lt;pre-compiled or not-compiled template file&gt; | grep "compilerInfo" 给我回了任何东西。但是,handlebars &lt;not-compiled template file&gt; | grep "compiler" 回复了我,"...compiler":[6,"&gt;= 2.0.0-beta.1"],"main":function(depth0,helpers,partials,data,depths... 所以它是 2.0.0 吗?该值如何来自非编译模板?我无法理解。
      • 这条评论很旧,但我正在回答记录。可能在那个版本的车把中命名发生了变化。你的似乎是2.0.0-beta.1。该值来自您通过使用handlebars &lt;not-compiled template file&gt; 执行车把编译器生成的编译模板。稍后,grep "compiler" 在 handlebars 编译器生成的输出中查找文本“compiler”,这要归功于管道。
      猜你喜欢
      • 2013-08-19
      • 1970-01-01
      • 2014-08-29
      • 2012-11-23
      • 2013-03-14
      • 2012-04-10
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      相关资源
      最近更新 更多