2016-09-14 6 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

エラーが戻ってきてもらうにはどうすればよいです?あなたのコードで小さなリポジトリを開始しましたが、エラー(実際には2回目のコンパイル時に発生しました)を見た後、戻すことはできません。 –

+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

それを逃しましたが、私はそれを修正しました。 –

関連する問題