Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dust resolve reference helper created and refactor if to use it #73

Merged
merged 1 commit into from Jul 2, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 18 additions & 12 deletions lib/dust-helpers.js
Expand Up @@ -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 );
Expand Down