句柄模板中的布尔逻辑 [英] boolean logic within a handlebars template

查看:97
本文介绍了句柄模板中的布尔逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



现在我用控制器功能来欺骗这个行为,所以我最终得到了控制器。 p>

  App.ApplicationController = Ember.Controller.extend({
bool1:true,
bool2:true,
both:function(){return this.bool1&& this.bool2;} .property('content.both'),
});

这允许我使用



的句柄模板

 < script type =text / x-handlebars> 
{{#both both}}
< p>都是真实的< / p>
{{/ if}}
< / script>

并且工作正常,但会引发一些问题。首先,它掩盖了发生了什么(特别是如果没有使用好的功能名称)。其次,它似乎侵犯了MVC的分离。



是否可以按照

 < script type =text / x-handlebars> 
{{#if bool1&&& bool2}}<! - 这实际上不会工作 - >
< p>都是真实的< / p>
{{/ if}}
< / script>

并让它工作?

解决方案

你不能直接做到这一点,但这不是很难与参数 解析和可变参数。这样的东西:

  Handlebars.registerHelper('if_all',function(){
var args = []。 slice.apply(arguments);
var opts = args.pop();

var fn = opts.fn;
for(var i = 0; i& .length; ++ i){
if(args [i])
continue;
fn = opts.inverse;
break;
}
return fn(this);
});

然后在模板中,您可以说:

  {{#if_all abc}} 

{{else}}

{{/ if_all}}

您可以使用尽可能多的参数 {{if_all}} 根据需要自 {{#if}} 对待

 `false`,'undefined`,'null`,` []`(一个falsy值)

as falsey, $ c> [] 在JavaScript中是真实的。



演示: http://jsfiddle.net/ambiguous/vrb2h/


Is it possible to perform boolean logic within a handlebars conditional?

Right now I spoof this behavior with a controller function, so I end up with the controller

App.ApplicationController = Ember.Controller.extend({
    bool1: true,
    bool2: true,
    both: function(){ return this.bool1 && this.bool2; }.property('content.both'),
});

Which allows me to use a handlebars template of

<script type="text/x-handlebars">
  {{#if both}}
     <p> both were true </p>
  {{/if}}
</script>

and that works fine, but raises some problems. First off, it obscures what's happening (particularly if good function names aren't used). Secondly, it seems to infringes a bit on the MVC separation.

Is it possible to do something along the lines of

<script type="text/x-handlebars">
  {{#if bool1 && bool2}}  <!-- this will not actually work -->
     <p> both were true </p>
  {{/if}}
</script>

and have it work?

解决方案

You can't do it directly but it isn't that hard to do with a little bit of arguments parsing and a variadic helper. Something like this:

Handlebars.registerHelper('if_all', function() {
    var args = [].slice.apply(arguments);
    var opts = args.pop();

    var fn = opts.fn;
    for(var i = 0; i < args.length; ++i) {
        if(args[i])
            continue;
        fn = opts.inverse;
        break;
    }
    return fn(this);
});

And then in a template you can say:

{{#if_all a b c}}
    yes
{{else}}
    no
{{/if_all}}

You can use as many arguments to {{#if_all}} as you need. You might want to adjust the truthiness test to match Handlebars since {{#if}} treats

`false`, `undefined`, `null`, `""` or `[]` (a "falsy" value)

as falsey and everything else as truthy whereas [] is truthy in JavaScript.

Demo: http://jsfiddle.net/ambiguous/vrb2h/

这篇关于句柄模板中的布尔逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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