Handlebars.registerHelper - 获取Block的内容的方式? [英] Handlebars.registerHelper - Way to get content of Block?

查看:123
本文介绍了Handlebars.registerHelper - 获取Block的内容的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



让我们假设我们有以下模板:

  {{myif test}}这是我想要的内容{{/ myif}} 

以下registerHelper代码:

  Ember.Handlebars.registerBoundHelper(' myif',function(test)
{
// do something
return< handlebars block> ;;
});

非常感谢!

解决方案

Handlebars为帮助者提供嵌套块作为 options.fn ,其中选项是你的帮手的最后一个论点。您可以使用上下文对象调用此块,该对象块将从哪里获取值。



要传递助手本身的上下文,您可以使用这个



在这种情况下,您可能还需要 options.inverse 这是一个可选的块,如果您的条件为false,将被使用。

  Ember.Handlebars.registerHelper('myif' ,function(condition,options){
if(condition){
return options.fn(this);
} else {
return options.inverse(this);
}
});

随后在模板中使用

  {{#myif condition}} 
true block here
{{else}}
else block here
{{/ myif} }


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!

解决方案

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}}

这篇关于Handlebars.registerHelper - 获取Block的内容的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆