Handlebars中的逻辑if / else语句? [英] Logic in Handlebars if/else statement?

查看:1638
本文介绍了Handlebars中的逻辑if / else语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我这个愚蠢的问题,我知道你不应该把把握逻辑放在把手的表情中,但我是新手,我不知道如何绕过它。

Forgive me for the stupid question, I know you're not supposed to put logic in handlebars expressions, but I'm new to this and I'm not sure how to get around it.

基本上我想根据手柄表达式的值更改按钮的文本,但我不知道如何在我的中添加逻辑运算符{{#if}} 声明。

Basically I want to change a button's text based on the value of a handlebars expression, but I'm not sure how to do so without being able to add logic operators in my {{#if}} statement.

基本上,当 {{form.target}} <的值时/ code>等于new,我希望按钮文本说保存,如果值等于编辑,我希望文本说保存更改。

Basically, when the value of {{form.target}} is equal to "new", I want the button text to say "Save", and if the value is equal to "edit', I want the text to say "Save Changes".

如果可以的话,这是我

This is the way I would write it if I could:

<button>
    {{#if {{form.target}} == 'new'}}
        Save
    {{#else if {{form.target}} == 'edit'}}
        Save Changes
    {{/if}} 
</button>

显然我离这里不远,所以我希望有人可以帮忙告诉我这样做的正确方法吗?

Obviously I'm way off base here, so I was hoping someone could point me to the correct way of doing this?

推荐答案

你可以注册帮助

<button>
    {{#equals form.target "new"}}
        Save
    {{else}}
        {{#equals form.target "edit"}}
            Save Changes
        {{/equals}} 
    {{/equals}} 
</button>

Handlebars.registerHelper("equals", function(string1 ,string2, options) {
    if (string1 === string2) {
        return options.fn(this);
    } else {
        return options.inverse(this);
    }
});

这篇关于Handlebars中的逻辑if / else语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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