gpt4 book ai didi

javascript - 或者使用 mustache.js 的等效运算符

转载 作者:数据小太阳 更新时间:2023-10-29 04:10:52 24 4
gpt4 key购买 nike

好的,所以我知道 mustache 模板没有逻辑,但我该如何实现:

<?php
if ($a || $b) {
echo $c, $d, $e;
}
?>

... 使用 mustache 模板语法?我能想到的最好的是:

{{#a}}
{{c}}{{d}}{{e}}
{{/a}}
{{^#a}}
{{#b}}
{{c}}{{d}}{{e}}
{{/b}}
{{/a}}

...这显然很可怕,需要我复制“if”中的任何内容。

有什么想法吗?

最佳答案

Mustache 明确禁止这样的事情。这是逻辑,您正试图将其放入您的模板中:)

适当的方法是将逻辑移动到您的 ViewModel 或 View 对象:

<?php
class MyView {
public $a;
public $b;
public function aOrB() {
return $this->a || $this->b;
}
}

但如果是我,我会将该函数命名为类似 hasFooshowBar 的名称,因此它具有一些语义意义。

因为您正在处理“我应该显示这个 block 吗?” View 或 ViewModel 中的逻辑,您将返回到模板中的正常部分:

{{#aOrB}}
{{c}}{{d}}{{e}}
{{/aOrB}}

关于javascript - 或者使用 mustache.js 的等效运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9737114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com