expressions and nested expressions in dust.js? -


i'd able add value in dust.js this:

 x+1 : {{x}+1} //does not work  :'( 

which know can helper (horrendously verbose)

 x+1 : {@math key="x" method="add" operand="1" /} 

which can live (but not happy)

but when want nest parameter?

 x+1+1 : {@math key='{@math key="x" method="add" operand="1" /}' method="add" operand="1" /} // no dice , wins ugly code prize!   x+1+1 : {@math key='x' method="add" operand="1"} {@math key="selectkey" method="add" operand="1" /} {/math}  //still no dice - btw selectkey output variable @math helper 

is possible this? i'm tempted try , patch in core lib because annoying me much.

what other ways there this? creating temporary variables (ie {xplus1})? current solution move any/all logic helpers - writing lot of helpers.


update:

i have written helper can create scoped variables. seems clean way it.

   {@eval xplus1="{x} + 1"}      ... scope x = x+1      x+1 : {xplus1}      {/eval}  

right using javascript eval, i'm considering using js math lib javascript expression evaluator or math.js

in addition tom's solution, here implementation of eval helper evaluates containing block.

for example, if have variable a = 5 , b = 10 in context,

{@eval}{a} + {b} + 100{/eval} 

would rendered 115.

you can compose evals:

{@eval}{a} + {b} + {@eval}{a} + {b} + 100{/eval}{/eval} //would render 130 

here code:

dust.helpers.eval = function(chunk, context, bodies) {     var expression = '';     chunk.tap(function(data) {         expression += data;         return '';     }).render(bodies.block, context).untap();     chunk.write(eval(expression));     return chunk; } 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -