TypeError应用预编译的Handlebars模板和上下文 [英] TypeError applying precompiled Handlebars templates with a context

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

问题描述

请原谅这个noob问题,但我根本无法获得预编译的Handlebars模板来执行任何操作,但barf out


$ b


TypeError:'undefined'不是一个函数(评估'templateSpec.call(容器,把手,上下文,options.helpers,options.partials,options.data)')



$ p
$ b

给定以下文件和内容:


  • hello.handlebars < p> Hello,{{name}}< / p>

  • templates.js :通过 handlebars hello.handlebars编译 hello.handlebars 的结果templates.js

  • index.html :includes 头部中的/handlebars.js/downloads\">Handlebars RC1运行时,以及正文:

     < body id =body> 
    < script src =templates.jstype =text / javascriptcharset =utf-8>< / script>
    < script type =text / javascriptcharset =utf-8>
    var compiledTemplate = Handlebars.templates ['hello'];
    var html = compiledTemplate({name:'World'});
    document.getElementById('body')。innerHTML = html;
    < / script>
    < / body>




调用 compiledTemplate )不管我做什么都会抛出这个错误 - 但是我能够获得客户端模板来编译和显示。我所见过的所有演练和教程都略过了这个,所以我必须错过一些愚蠢的东西。任何想法?

解决方案

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


句柄a_template_of_yours.handlebars | grepcompilerInfo

它应该输出如下内容:

$

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


Handlebars.COMPILER_REVISION


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


npm install handlebars@1.3。 0 -g


Pardon the noob question but I simply can't get precompiled Handlebars templates to do anything but barf out

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

each time I apply a context to a (precompiled) template.

Given the following files and contents:

  • hello.handlebars: <p>Hello, {{name}}</p>
  • templates.js: the result of compiling hello.handlebars via handlebars hello.handlebars -f templates.js
  • index.html: includes Handlebars RC1 runtime in the head and this for the body:

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

Calling compiledTemplate() throws that error above no matter what I do--yet I'm able to get client-side templates to compile and display just fine. All the walkthroughs and tutorials I've seen skip through this like its obvious so I must be missing something silly. Any ideas?

解决方案

Be sure to match the versions of the Handlebars server and client packages. A way to check the version of the compiled template is the following:

handlebars a_template_of_yours.handlebars | grep "compilerInfo"

It should output something like this:

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

The first value is the Compiler Revision, which should match the output of the following code on your browser:

Handlebars.COMPILER_REVISION

If you used npm to download your Handlebars compiler, specify the version which you need. Here is an example for handlebars.runtime-v1.3.0.js:

npm install handlebars@1.3.0 -g

这篇关于TypeError应用预编译的Handlebars模板和上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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