2016-09-14 44 views
1

我有一個節點腳本可以使用車把來編譯模板。這裏是我的模板:把手:第一次運行時未發現局部變形

<div class="header"> 
    <h1>{{title}}</h1> 
</div> 
<div class="body"> 
    <p>{{body}}</p> 
</div> 
<div class="footer"> 
    <div><a href="http://twitter.com/{{author.twitter}}">{{autor.name}}</a> 
    </div> 
    <ul> 
     {{#each tags}} 
     <li>{{this}}</li> 
     {{/each}} 
    </ul> 
    {{> example_partial}} 
</div> 

相應的部分是

<div> 
    <p> 
    Hi, I am a partial! 
    </p> 
</div> 

而且JS

var handlebars = require('handlebars'), 
    fs = require('fs'); 

var data = { 
    title: 'practical node.js', 
    author: '@azat_co', 
    tags: ['express', 'node', 'javascript'] 
} 
data.body = process.argv[2]; 

fs.readFile('handlebars-example-partial.html', 'utf-8', function(error, source) { 
    handlebars.registerPartial('example_partial', source); 
}); 

fs.readFile('handlebars-example.html', 'utf-8', function(error, source){ 

    var template = handlebars.compile(source); 
    var html = template(data); 
    console.log(html) 
}); 

我真的不能找出然後當我通過節點運行該腳本的第一次使用node app.js,我得到以下錯誤:

/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266 
    throw new _exception2['default']('The partial ' + options.name + ' could not be found'); 
    ^
Error: The partial example_partial could not be found 
    at Object.invokePartial (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:266:11) 
    at Object.invokePartialWrapper [as invokePartial] (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:68:39) 
    at Object.eval (eval at createFunctionContext (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/compiler/javascript-compiler.js:254:23), <anonymous>:16:28) 
    at main (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:173:32) 
    at ret (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/runtime.js:176:12) 
    at ret (/Users/rahul/stencil/node_modules/handlebars/dist/cjs/handlebars/compiler/compiler.js:525:21) 
    at /Users/rahul/stencil/examples/standalone_v1/handlebars-example.js:29:14 
    at tryToString (fs.js:414:3) 
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:401:12) 

但是,當我再次運行該程序時,它工作正常,我得到預期的輸出(不更改任何內容)。有人可以向我解釋我做錯了什麼嗎?

+0

你如何錯誤回來?我已經用你的代碼開始了一個小回購,但在看到錯誤(實際發生在第二次編譯)後,我無法恢復。 –

+0

嘿@adam-beck,如果我編輯js文件,它通常會返回給我。 – rahulthewall

回答

1

問題是您的部分實際上並沒有在您編譯使用它的模板時進行註冊。這是因爲fs.readFile是一個異步操作。

一個解決方案是使用fs.readFileSync

var partial = fs.readFileSync('handlebars-example-partial.html', 'utf-8'); 
handlebars.registerPartial('example_partial', partial); 

fs.readFile('handlebars-example.html', 'utf-8', function(error, source){ 
    var template = handlebars.compile(source);                                                 
    var html = template(data);                                                     
    console.log(html) 
}); 

或者你可以把它所有的部分進行登記的回調:

fs.readFile('handlebars-example-partial.html', 'utf-8', function(error, partial) { 
    handlebars.registerPartial('example_partial', partial); 
    fs.readFile('handlebars-example.html', 'utf-8', function(error, template) { 
    var compiled = handlebars.compile(template); 
    var html = compiled(data); 
    console.log(html); 
    }); 
} 
+0

謝謝,現在我知道出了什麼問題。上面的代碼只是一個小小的修改(對於任何複製粘貼它的人)) 'handlebars.registerPartial('example_partial',source);'應該是 'handlebars.registerPartial('example_partial',partial);' – rahulthewall

+1

糟糕!錯過了,但我糾正了它。 –

相關問題