From 63d55c0063543b44455fe832aed50a2b348ec53d Mon Sep 17 00:00:00 2001 From: Jairo de Morais Date: Mon, 2 Jul 2012 11:45:02 -0300 Subject: [PATCH] dust resolve reference helper created and refactor if to use it --- lib/dust-helpers.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/dust-helpers.js b/lib/dust-helpers.js index d8b7b5d6..4a2120cf 100644 --- a/lib/dust-helpers.js +++ b/lib/dust-helpers.js @@ -72,22 +72,28 @@ var helpers = { contextDump: function(chunk, context, bodies) { _console.log(JSON.stringify(context.stack)); return chunk; + },// Utility helping to resolve dust references in the given chunk + tap: function( input, chunk, context ){ + // return given input if there is no dust reference to resolve + var output = input; + // dust compiles a string to function, if there are references + if( typeof input === "function"){ + output = ''; + chunk.tap(function(data){ + output += data; + return ''; + }).render(input, context).untap(); + if( output === '' ){ + output = false; + } + } + return output; }, + "if": function( chunk, context, bodies, params ){ if( params && params.cond ){ var cond = params.cond; - - // resolve dust references in the expression - if( typeof cond === "function" ){ - cond = ''; - chunk.tap( function( data ){ - cond += data; - return ''; - } ).render( params.cond, context ).untap(); - if( cond === '' ){ - cond = false; - } - } + cond = this.tap(cond, chunk, context); // eval expressions with no dust references if( eval( cond ) ){ return chunk.render( bodies.block, context );