问题
is there a way in registerHelper to get the content of a block?
Lets assume we have the following template:
{{#myif test}}thats the content i want to have{{/myif}}
And the following registerHelper Code:
Ember.Handlebars.registerBoundHelper('myif', function(test)
{
// do something
return <content of handlebars block>;
});
Many thanks!
回答1:
Handlebars provides the nested block to the helper as options.fn, where options is the last argument of your helper. You can invoke this block with a context object which is where that block will pick up values from.
To pass the context of the helper itself you can call it with this.
In this case you will probably also want options.inverse which is an optional block that will be used if your condition is false.
Ember.Handlebars.registerHelper('myif', function(condition, options) {
if (condition) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
And the subsequent use in the template,
{{#myif condition}}
true block here
{{else}}
else block here
{{/myif}}
来源:https://stackoverflow.com/questions/17632386/handlebars-registerhelper-way-to-get-content-of-block