how to write some variable(name and value) in dust template using helper

1.1k Views Asked by At


I have client side dust template and a corresponding helper.

Helper :

function(chunk, context, bodies, params) {
};

Now I want to write some key value pair(from helper) which I can read in dust template.
e.g.If i write (k,v) in helper, in dust template

{k}

should output 'v'

Thanks,
Shantanu

1

There are 1 best solutions below

0
On

A helper in Dust receives the current chunk and context. You can push new values onto the context stack, and then call chunk.render().

{
  "helper": function(chunk, context, bodies, params) {
    var obj = { "k": "value" };
    return chunk.render(bodies.block, context.push(obj));
  }
}

Here, I am rendering the default body (bodies.block), using the existing context as well as my extra obj, which is pushed onto the context stack.

{#helper}{k}{/helper} {! k is only accessible inside the context of my helper !}