javascript 或使用 mustache.js 的运算符等效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9737114/
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
Or operator equivalent using mustache.js
提问by Stephen Sorensen
Ok, so I know that mustache templates don't have logic, but how do I implement this:
好的,所以我知道 mustache 模板没有逻辑,但是我该如何实现:
<?php
if ($a || $b) {
echo $c, $d, $e;
}
?>
... using mustache template syntax? The best I can come up with is this:
...使用 mustache 模板语法?我能想到的最好的是:
{{#a}}
{{c}}{{d}}{{e}}
{{/a}}
{{^#a}}
{{#b}}
{{c}}{{d}}{{e}}
{{/b}}
{{/a}}
... which is obviously hideous and requires me to duplicate anything inside the 'if'.
...这显然是可怕的,需要我在“if”中复制任何内容。
Any ideas?
有任何想法吗?
回答by bobthecow
Mustache expressly forbids things like this. That's logic, and you're trying to put it in your template :)
Mustache 明确禁止这样的事情。这是逻辑,你正试图把它放在你的模板中 :)
The appropriate way would be to move the logic to your ViewModel or View object:
适当的方法是将逻辑移动到您的 ViewModel 或 View 对象:
<?php
class MyView {
public $a;
public $b;
public function aOrB() {
return $this->a || $this->b;
}
}
But if it were me, I'd name that function something like hasFoo
or showBar
, so it has a bit of semantic meaning.
但如果是我,我会把这个函数命名为hasFoo
or showBar
,所以它有一点语义。
Because you're handling the "should I show this block?" logic in your View or ViewModel, you're back to a normal section in your template:
因为你正在处理“我应该展示这个块吗?” 逻辑在您的视图或视图模型中,您将返回到模板中的正常部分:
{{#aOrB}}
{{c}}{{d}}{{e}}
{{/aOrB}}
回答by ambe5960
For the record, that is the only way to do it with mustache. As for now (mustache 5, I believe) there is no better solution.
根据记录,这是留胡子的唯一方法。至于现在(小胡子 5,我相信)没有更好的解决方案。