songyy
songyy

Reputation: 4563

add Variable into Context, or define local variable, in DustJS?

In my dustJS template, I make use of a combination of variable to use it as a new variable. For example, say if I have pname and cname, then I want to create name = pname + cname. Additionally.. I may want to create local variable based on certain condition, for example, name = {?.rep} pname {:else} pname + name {/.rep}. I may want to pass these variables into helpers.

To stick to the DRY principle, the best option would be to create local so that I can refer to this local variable later when it appears again.

I tried out with partials, something like: {<name}pname + cname{/name}, and use it later as {+name/}. But this way doesn't work with helpers, as well as the conditional block.

I think one possible solution might be that.. adding variables into the current context using helpers, but I don't know how to do.

So my question here is: how to add variable into current context, or how to define a local variable on the fly?

Thanks for your help.

Upvotes: 2

Views: 1568

Answers (1)

kate2753
kate2753

Reputation: 136

@provide helper from dust-motes repo should help you solve this issue.

If for some reason @provide helper does not work for you, take a look at the proposed @addToContext helper in dustjs-helpers repo.@addToContext helper modifies context, so if you are not careful it might override context data with the same keys. @provide helper is preferred to @addToContext.

Upvotes: 1

Related Questions