Handlebars.registerHelper - Way to get content of Block?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 03:02:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!