2014-02-23 24 views
21

原諒noob問題,但我根本無法預編譯把手模板做任何事情,但我每個應用上下文的(預編譯)模板的時間BARF出類型錯誤將預編譯的把手模板與上下文

TypeError: 'undefined' is not a function (evaluating '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的結果.html:包括Handlebars RC1 runtimehead,這對於身體:

    <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()拋出錯誤上面不管我做什麼 - 但我能夠獲得客戶端模板編譯並顯示就好了。我所見過的所有演練和教程都略過了這個,所以我必須錯過一些愚蠢的東西。有任何想法嗎?

回答

27

一定要匹配Handlebars服務器和客戶端軟件包的版本。檢查編譯模板的版本的方法如下:

handlebars a_template_of_yours.handlebars | grep "compilerInfo"

它應該輸出是這樣的:

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

的第一個值是編譯器版本,這應該匹配的輸出您的瀏覽器下面的代碼:

Handlebars.COMPILER_REVISION

如果您使用NPM下載您手把編譯器,指定VERS你需要的離子。這裏是handlebars.runtime-v1.3.0.js一個例子:

npm install [email protected] -g

+7

這幫了我。 npm默認安裝'handlebars-2.0.0-alpha' ...這與運行時不兼容1.3.0 – otmezger

+0

我正在運行[1.0.rc.1](https://github.com/wycats/handlebars .js/downloads)。 – ele

+0

@ele,我建議你 - 如果可能的話 - 升級到Handlebars的更新版本,因爲1.0.rc.1的日期從2012年9月開始。最新的穩定版本從2014年1月起是1.3.0,因此可能會有很多改進你也會失蹤。 您可以從[Handlebars Home](http://handlebarsjs.com/)下載最新的穩定客戶端(即運行時js模塊)。 節點編譯器(服務器模塊)可以從npm下載(指定版本,以防止下載一些新的alpha版本),或從[Github發佈網站](https://github.com/wycats/handlebars.js /釋放)。 – biomorgoth

3

這個問題對我來說,目前(2014年6月),在GitHub上的2.0版本被標記阿爾法https://github.com/wycats/handlebars.js/releases

如果我通過bower安裝把手,它會選擇最新的穩定版本。 v1.3.0。但是,安裝~2.0.0-alpha.4版本。

解決方案:更新bower.json的版本~2.0.*,並運行bower update