Add #each and #if built-in helpers for more readable iteration and conditionals
This commit is contained in:
@@ -77,6 +77,28 @@ Handlebars.registerHelper('blockHelperMissing', function(context, fn, inverse) {
|
||||
}, function(context, fn) {
|
||||
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)
|
||||
|
||||
exports.Handlebars = Handlebars;
|
||||
|
||||
Reference in New Issue
Block a user