Dust Helpers

Function definition

dust.helpers.myHelper = function(chunk, context, bodies, params) {
   /* logic here */
   return chunk;
}

Parameters

Example

In this example, we will write a helper that will add a period to the end or beginning of the body depending on a parameter.

JavaScript function definition for a helper

dust.helpers.period = function(chunk, context, bodies, params) {
  var location = params.location,
      body = bodies.block;
  if (location === 'start') {
    chunk.write('.');
    chunk.render(body, context);
  } else if (location === 'end') {
    chunk.render(body, context);
    chunk.write('.');
  } else {
    dust.log('WARN', 'missing parameter "location" in period helper');
  }
  return chunk;
};

Helper call in the template:

{@period location="end"}
  Hello World
{/period}
{!-- outputs "Hello World." --!}

{@period location="start"}
  Hello World
{/period}
{!-- outputs ".Hello World" --!}

Fork me on GitHub