javascript handlebars.js 中是否有三元运算符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11915839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:44:37  来源:igfitidea点击:

Is there a ternary operator in handlebars.js?

javascripttemplateshandlebars.js

提问by shawnXiao

In Handlebars, is there a ternary operator? I don't mean if else; I mean like a == true ? "a" : "b".

在 Handlebars 中,是否有三元运算符?我不是说if else;我的意思是像a == true ? "a" : "b"

采纳答案by dignifiedquire

You can build your own helper in handlbars if you really want to. Something like ternary(a==true, "a", "b"). For more information on that see the documentation. The idea from m90 is not the idea behind handlebars. The idea is to not have explicit code in your templates, only calls to helpers and objects.

如果您真的愿意,您可以在handlbars 中构建自己的助手。类似的东西ternary(a==true, "a", "b")。有关更多信息,请参阅文档。m90 的想法不是车把背后的想法。这个想法是在你的模板中没有显式代码,只调用助手和对象。

回答by Chris

The ifhelper can be used as a ternary operator by passing three arguments to it.

通过if向其传递三个参数,帮助器可以用作三元运算符。

In the following example, a button has a default value of "Save Changes", but when model.isSavingis true, then the value temporarily changes to Saving....

在以下示例中,按钮的默认值为"Save Changes",但当model.isSaving为 true 时,该值会临时更改为Saving...

<button>{{if model.isSaving "Saving..." "Save Changes"}}</button>

...alternatively, used within another helper:

...或者,在另一个助手中使用:

{{input type="submit" value=(if model.isSaving "Saving..." "Save Changes")}}

回答by Terion

I have a helper for this (pay attention that other helpers can also be used inside) https://gist.github.com/terion-name/d87ed8907f1bb2e25f32

我有一个帮手(注意里面也可以使用其他帮手) https://gist.github.com/terion-name/d87ed8907f1bb2e25f32

// app/helpers/iftrue.js
import Ember from 'ember';

export function iftrue(params) {
  if (params[0]) {
    return params.length === 2 ? params[0] : params[1];
  }
  if (params.length === 2) {
    return params[1];
  } else if (params.length === 3) {
    return params[2];
  }
  return null;
}

export default Ember.Helper.helper(iftrue);

With two parameters: if first parameter evaluates to true it will be printed, otherwise second

有两个参数:如果第一个参数计算结果为真,它将被打印,否则第二个

{{iftrue project.price 'N/A'}} // .99
{{iftrue project.priceNotAvailable 'N/A'}} // N/A

With three parameters: if first parameter evaluates to true second will be printed, otherwise third

带三个参数:如果第一个参数计算结果为真,则打印第二个,否则打印第三个

// If deadline is set formatted date will be printed, otherwise 'N/A'
{{iftrue project.deadline (moment-format project.deadline 'DD.MM.YYYY') 'N/A'}} 

回答by Pram

This below code can be used for ternary or any kind of expression eval.

下面的代码可用于三元或任何类型的表达式 eval。

Warning: please use this code in scenario where eval can be used safely.

警告:请在可以安全使用 eval 的场景中使用此代码。

{{#if (myfunc "(a[0] + 1) % 2 === 0" arg1)}}

{{/if}}

{{#if (myfunc "(a[0] + a[1]) % 2 === 0" arg1 arg2)}}

{{/if}}

handlebar helper function

车把辅助功能

myfunc: (exp, ...a) => {
    return eval(exp);
  }