Search code examples
javascriptnode.jstemplatesdust.jsnw.js

Dust js partials / child templaes output undefined


I'm using nwjs and dust.js. Parent templates work as expected, but if I try to load a child (partial) template the output is undefined. Am I missing something to get child/partial templates working? Thanks.

<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>

Templates:

someTemplate.dust: (parent)

<h1>{heading}</h1>
<p>{article}</p>
{+someBlock/}

anotherTemplate.dust: (child/partial)

{>someTemplate/}
{<someBlock}
    My custom block content.
{/someBlock}

Compiled templates: (compiled with dustc - ./node_modules/dustjs-linkedin/bin/dustc ./src/templates/*.dust -o ./dist/templates.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));

Solution

  • You're including {>someTemplate/}, but you've registered the template with a different name: src/templates/someTemplate.

    If you want Dust to register your templates without the src/templates prefix, pass the --pwd parameter to dustc:

    ./node_modules/dustjs-linkedin/bin/dustc ./src/templates/*.dust -o ./dist/templates.js --pwd=src/templates