DustJS - variable in the template

I have a function that creates a random number.

dust.helpers.randomNumber = function(chunk, ctx, bodies, params) {
    return chunk.write(Math.floor((Math.random() * 1000) + 1));
};

And in the template, I will call:

{@randomNumber/}

It works, but I want to assign the return of this function to a variable in tempalte. I need to use the same random numbers in 2-4 places.

I have found a solution. I am expanding my functioning:

dust.helpers.randomNumber = function(chunk, ctx, bodies, params) {
        var number = Math.floor((Math.random() * 1000) + 1);
        if(params !== null && params.assignToProp){
            ctx.stack.head[params.assignToProp] = number;
            return chunk.write("");
        }else
            return chunk.write(number);
};

And in the template call:

{@randomNumber assignToProp="_idSuffix"/}

And this variable is under:

{._idSuffix}
+4
source share

All Articles