私はnwjsとdust.jsを使用しています。親テンプレートは期待どおりに動作しますが、子(部分)テンプレートをロードしようとすると、出力は未定義です。私は子供/部分テンプレートが動作するように何かを見逃していますか?ありがとう。ダストjs部分/子テンプレート出力が未定義
<div id="output"></div>
<script>
const dust = require('dustjs-helpers');
</script>
<!-- Because I can't figure out why require(./dist/templates) errors dust undefined. -->
<script type="text/javascript" src="./dist/templates.js"></script>
<script type="text/javascript">
let templateName = 'src/templates/anotherTemplate',
data = {heading: "This is a heading", article: "Blah blah blah."};
dust.render(templateName, data, (err, out) => {
console.log(out);
document.getElementById('output').innerHTML = out;
});
</script>
テンプレート:
someTemplate.dust:(親)
<h1>{heading}</h1>
<p>{article}</p>
{+someBlock/}
anotherTemplate.dust:(子/部分)
{>someTemplate/}
{<someBlock}
My custom block content.
{/someBlock}
コンパイルされたテンプレート: (とコンパイルdustc - ./node_modules/dustjs-linkedin/bin/dustc ./src/templates/*.dust -o ./dist/templat es.js)
(function(dust){dust.register("src\/templates\/anotherTemplate",body_0);var blocks={"someBlock":body_1};function body_0(chk,ctx){ctx=ctx.shiftBlocks(blocks);return chk.p("someTemplate",ctx,ctx,{});}body_0.__dustBody=!0;function body_1(chk,ctx){ctx=ctx.shiftBlocks(blocks);return chk.w("My custom block content.");}body_1.__dustBody=!0;return body_0}(dust));
(function(dust){dust.register("src\/templates\/someTemplate",body_0);function body_0(chk,ctx){return chk.w("<h1>").f(ctx.get(["heading"], false),ctx,"h").w("</h1><p>").f(ctx.get(["article"], false),ctx,"h").w("</p>").b(ctx.getBlock("someBlock"),ctx,{},{});}body_0.__dustBody=!0;return body_0}(dust));
それはあなたが)(あなたのほこりを必要とする場合comment--を残して、この質問のポイントではありませんので、テンプレートを作成するには、 '--cjs'を使用してCommonJSモジュールとしてコンパイルします(ただし、各テンプレートは別々に指定する必要があります):http://www.dustjs.com/guides/rendering/#node-load-precompiled-templates – Interrobang
CommonJSの例:https://github.com/linkedin/dustjs/tree/master/examples/commonjs – Interrobang