peduarte
peduarte

Reputation: 1677

DustJS: Render partial via a Helper

I'm trying to abstract some code and would like to take advantage of dust.helpers to render a parial.

My current setup:

{> "includes/components/link" /}

My ideal setup:

{@uiComponent name="link" /}

My helper:

dust.helpers.uiComponent = function (chunk, context, bodies, params) {
    return dust.render('includes/components/' + name, context, function (err, out) {
        chunk.end(out);
    });
};

I have also tried a number of other things and nothing works.

And yes, I tried looking at the documentation. :(

Any advice would be much appreciated!

Upvotes: 0

Views: 339

Answers (1)

Interrobang
Interrobang

Reputation: 17434

In Dust, helpers return Chunks, so you want to use Chunk methods to return out of your helper rather than dust.render.

In this case, you are working with partials, so you want chunk.partial:

dust.helpers.uiComponent = function (chunk, context, bodies, params) {
  var name = context.resolve(params.name);
  return chunk.partial('includes/components/' + name, context, params);
};

Upvotes: 1

Related Questions