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

Custom inline block will make "{{else}}" block more useful #266

Closed
wants to merge 1 commit into from
Closed

Conversation

RocHo
Copy link

@RocHo RocHo commented Jun 28, 2012

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

{{#if isActive}}
Active
{{else}}
Inactive
{{/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}}
Active
{{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?

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

{{#if isActive}}
  <img src="star.gif" alt="Active">
{{else}}
  <img src="cry.gif" alt="Inactive">
{{/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}}
  <img src="star.gif" alt="Active">
{{step}}
  <span>|</span>
{{header}}
  <h3>Rate this</h3>
{{footer}}
  <div class='clearfix' />
{{/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?
@wagenet
Copy link
Collaborator

wagenet commented Jul 31, 2012

This seems interesting but I don't think it's something @wycats is likely to implement. IIRC he's already not 100% happy about inverse. I don't see what's stopping you from just defining normal helpers that can work inside of the block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants