【问题标题】:how to: mustache if else statement如何:如果 else 语句中的小胡子
【发布时间】:2016-07-28 21:27:18
【问题描述】:

下面的小胡子怎么写?

我想检查一个变量是否存在,如果存在,我想检查那个变量是否等于另一个变量。所以

这将是 PHP 的等价物

if ($example && $example == $type) {
}

【问题讨论】:

    标签: mustache


    【解决方案1】:

    或者,小胡子短代码使用 # 符号表示“如果”,因此假设您的短代码是 {{example}} 然后

    {{#example}} 
        Start condition if example exists
    {{/example}}
    

    对于“如果不存在”,^ 的工作方式相同

    {{ ^example}}
        Start condition if example does not exist
    {{/example}}
    

    注意 - 如果使用 mustache 条件,您必须记住以与 HTML 属性相同的方式关闭条件。

    【讨论】:

      【解决方案2】:

      如果您使用 mustache,则目标是在控制器中执行此类逻辑,而不是在模板中。根据How do I accomplish an if/else in mustache.js?,您可能希望在控制器中进行此类检查并生成一个可以在模板中使用的布尔值

      【讨论】:

        【解决方案3】:

        您可以将闭包附加到小胡子数据并将此逻辑放入该闭包中,例如:

        var md = {};
        md['my_condition'] = function(){
          return (this['some_attr'] && this['some_attr'] == 'expected_val');
        }
        

        然后使用 md 对象作为 mustache 模板的数据。

        【讨论】:

          猜你喜欢
          • 2017-07-02
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-18
          • 2015-04-27
          • 1970-01-01
          相关资源
          最近更新 更多