wycats/handlebars.js — pull request #266 by RocHo (2012-06-28 08:03:19+00:00)
Title: Custom inline block will make "{{else}}" block more useful

I think we could make "{{else}}" block more useful

{{#if isActive}}

{{else}}

{{/if}}
Handlebars.registerHelper('if', function(conditional, options) {
if(conditional) {
return options.fn(this);
} else {
return options.inverse(this);
}
});

We can use `options.inverse` to receive the `{{else}}` block defined in `{{#if}}` block helper.
But I found that the compiler only handle `{{else}}` block.
Why couldn't we expand this to a new level, let us to define custom inline block?

Something like:

{{#for items}}

{{step}}
|
{{header}}

Rate this

{{footer}}

{{/for}}

We can use these custom block definitions in helper function

Handlebars.registerHelper('for', function(items, options) {
var r = "";
if(items && items.length) {
r += options.header(this);
for(var i=items.length;i < items.length;i++){
r += options.fn(items[i]);
if(i!==items.length-1){
r += options.step(items[i]);
}
}
r += options.header(this);
}
return r;
});

What do you think?