TypeError应用带有上下文的预编译Handlebars模板
赦免noob问题,但我根本无法得到预编译Handlebars模板做任何事,但barf出
TypeError:'undefined'不是一个函数(评估'templateSpec.call(容器,Handlebars,上下文,options.helpers,options.partials,options.data)')
每次我将一个上下文应用到(预编译的)模板。
鉴于以下文件和内容:
- hello.handlebars :
<p>Hello, {{name}}</p>
- templates.js :通过
handlebars hello.handlebars -f templates.js
编译hello.handlebars的结果handlebars hello.handlebars -f templates.js
-
index.html :在
head
包含Handlebars RC1运行时 ,并且包含正文:<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()
会抛出上面的错误,无论我做什么 – 但我能够获得客户端模板编译和显示就好了。 所有的演练和教程,我已经看过像这样明显的跳过,所以我必须错过一些愚蠢的东西。 有任何想法吗?
一定要匹配Handlebars服务器和客户端软件包的版本。 检查编译模板版本的方法如下:
把手a_template_of_yours.handlebars | grep“compilerInfo”
它应该输出这样的东西:
this.compilerInfo = [4,'> = 1.0.0'];
第一个值是编译器版本,它应该与浏览器上的以下代码的输出相匹配:
Handlebars.COMPILER_REVISION
如果您使用npm下载您的Handlebars编译器,请指定您需要的版本。 下面是一个handlebars.runtime-v1.3.0.js的例子:
npm install handlebars@1.3.0 -g
对我来说,问题是,目前(2014年6月),GitHub上的2.0版本标记为alpha https://github.com/wycats/handlebars.js/releases 。
如果我通过bower
安装把手,它会select最新的稳定版本。 v1.3.0
。
npm install handlebars
,但是,安装~2.0.0-alpha.4
版本。
解决scheme:将bower.json
的版本更新为bower.json
~2.0.*
,然后运行bower update
。