2014-09-29 38 views
14

I have published all my code as a runnable that uses express for loading static content未知模板對象錯誤2.0運行

我已預編譯此車把模板:

<img src="{{coverImage}}"/> 
<ul> 
    <li>{{title}}</li> 
    <li>{{author}}</li> 
    <li>{{releaseDate}}</li> 
    <li>{{keywords}}</li> 
</ul> 

刪除

我已獲得此功能:

(function() { 
    var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; 
templates['bookTemplate'] = template(function (Handlebars,depth0,helpers,partials,data) { 
    this.compilerInfo = [4,'>= 1.0.0']; 
helpers = this.merge(helpers, Handlebars.helpers); data = data || {}; 
    var buffer = "", stack1, helper, functionType="function", escapeExpression=this.escapeExpression; 


    buffer += "<img src=\""; 
    if (helper = helpers.coverImage) { stack1 = helper.call(depth0, {hash:{},data:data}); } 
    else { helper = (depth0 && depth0.coverImage); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; } 
    buffer += escapeExpression(stack1) 
    + "\"/>\r\n <ul>\r\n  <li>"; 
    if (helper = helpers.title) { stack1 = helper.call(depth0, {hash:{},data:data}); } 
    else { helper = (depth0 && depth0.title); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; } 
    buffer += escapeExpression(stack1) 
    + "</li>\r\n  <li>"; 
    if (helper = helpers.author) { stack1 = helper.call(depth0, {hash:{},data:data}); } 
    else { helper = (depth0 && depth0.author); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; } 
    buffer += escapeExpression(stack1) 
    + "</li>\r\n  <li>"; 
    if (helper = helpers.releaseDate) { stack1 = helper.call(depth0, {hash:{},data:data}); } 
    else { helper = (depth0 && depth0.releaseDate); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; } 
    buffer += escapeExpression(stack1) 
    + "</li>\r\n  <li>"; 
    if (helper = helpers.keywords) { stack1 = helper.call(depth0, {hash:{},data:data}); } 
    else { helper = (depth0 && depth0.keywords); stack1 = typeof helper === functionType ? helper.call(depth0, {hash:{},data:data}) : helper; } 
    buffer += escapeExpression(stack1) 
    + "</li>\r\n </ul>\r\n<button class=\"delete\">Delete</button>"; 
    return buffer; 
    }); 
})(); 

我有添加了這樣的腳本:

<script src="js/lib/handlebars.runtime-v2.0.0.js"></script> 
<script src="js/templates.js"></script> 

我在Chrome Dev Tools看到這兩個腳本加載。

然而,當我使用它是這樣的:

//inside a Backbone view 
template:Handlebars.templates.bookTemplate 

我得到這個錯誤:

Uncaught Error: Unknown template object: function handlebars.runtime-v2.0.0.js:455template handlebars.runtime-v2.0.0.js:455hb.template handlebars.runtime-v2.0.0.js:644(anonymous function) templates.js:3(anonymous function) 

我得到線455這個錯誤在handlebars runtime @2.0.0在功能template,在做一個小調試我發現templateSpec是一個函數,但templateSpec.mainundefined

//this function makes a call to templates['bookTemplate'] = template(function (Handlebars,depth0,helpers,partials,data) {...} in the `bookTemplate.js` 
    function template(templateSpec, env) { 
/* istanbul ignore next */ 
if (!env) { 
    throw new Exception("No environment passed to template"); 
} 
//error occurs here: 
if (!templateSpec || !templateSpec.main) { 
    throw new Exception('Unkn own template object: ' + typeof templateSpec); 
} 
... 

此外,我發現Handlebars.templates是一個空對象。

這是怎麼回事?

+1

任何在[here](https://github.com/leshill/handlebars_assets/issues/110)聽起來很熟悉?其他任何[Google點擊](https://www.google.ca/#q=handlebars+Uncaught+Error%3A+Unknown+template+object)如何? – 2014-09-29 23:39:57

+0

@ muistooshort我在Chrome中禁用瀏覽器緩存,谷歌點擊太複雜,我做的很簡單,我預編譯一個句柄模板,並使用它與運行時庫,沒有服務器,使用'file://',更新顯示錯誤位置的帖子 – vamsiampolu 2014-09-30 01:51:16

+0

@ muistooshort'templateSpec.main'是'undefined' – vamsiampolu 2014-09-30 02:02:01

回答

7

如果您使用handlebars 1.3.x預編譯了該模板,那麼您將遇到2.0的重大更改。確保服務器/構建工具正在運行2.0句柄編譯器。

+0

您是否也可以添加'handlebars' npm模塊必須全局安裝 – vamsiampolu 2014-12-16 00:18:06

+1

一般來說,這不是一個硬性和快速的要求。如果使用js構建系統,它通常只是把它作爲它的hiearchy中的一個需求。即使你在項目中使用了Makefile,你也可以引用'node_modules/.bin/handlebars'而不是依靠全局安裝。如果使用npm腳本,它更加容易,因爲您可以參考_package.json腳本部分中的depennency可執行文件,而無需指定到node_modules的路徑。 – jwilm 2014-12-16 16:40:47

+0

@vamsiampolu'handlebars' npm模塊根本不需要安裝。你甚至不需要顯式安裝把手涼亭組件。 – ohcibi 2015-02-01 15:58:42

0

您確定您編譯的模板使用與運行時相同的Handlebars版本嗎?

我只是嘗試Handlebars 2.0,我編譯的模板在編譯器字段中有[6,'>= 2.0.0']。你的編譯模板表明它是用Handlebars 1.x編譯的。

+0

我不明白你爲什麼刪除了我的答案,因爲: 1 /它是正確的(請參閱接受的答案) 2 /我無法對帖子發表評論無論如何,這不是我的。 – Benito 2014-12-16 00:48:43