Add #each and #if built-in helpers for more readable iteration and conditionals

This commit is contained in:
wycats
2010-12-19 00:06:55 -08:00
parent f4ae0c27fe
commit 8f502ffbe7
+22
View File
@@ -77,6 +77,28 @@ Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
}, function(context, fn) { }, function(context, fn) {
return fn(context) return fn(context)
}); });
Handlebars.registerHelper('each', function(context, fn, inverse) {
var ret = "";
if(context.length > 0) {
for(var i=0, j=context.length; i<j; i++) {
ret = ret + fn(context[i]);
}
} else {
ret = inverse(this);
}
return ret;
});
Handlebars.registerHelper('if', function(context, fn, inverse) {
if(context === false || context == null) {
return inverse(this);
} else {
return fn(this);
}
});
// END(BROWSER) // END(BROWSER)
exports.Handlebars = Handlebars; exports.Handlebars = Handlebars;