【问题标题】:If statement within another if statement in Handlebars templatesHandlebars 模板中另一个 if 语句中的 if 语句
【发布时间】:2013-06-05 11:17:01
【问题描述】:

这些都是我传递给车把模板的对象。

self.template = template({ data: self.model, lang:self.lang, page:self.mainPage, subpage:self.param }); 

然后在 foreach 循环中我有一个 if 语句,我无法访问父上下文元素。我在模板中的代码和问题如下所示:

    {{#each data}}  
        <h1>{{../subpage}}</h1> --> This is giving me a subpage value
        {{#if this.brand}} 

                  {{#if ../subpage}}
                    <h1>WHY IS THIS NOT SHOWING?</h1> --> This is what i am trying to achieve
                  {{/if}}

        {{/if}}  
    {{/each}}

任何想法如何实现这一目标?非常感谢您的所有回答/cmets!

【问题讨论】:

    标签: javascript handlebars.js template-engine


    【解决方案1】:

    {{#if this.brand}} 进入另一个级别的上下文,因此您必须转义到父上下文不是一次,而是两次。

    因此,您的模板应该是:

    {{#each data}}  
        <h1>{{../subpage}}</h1>
        {{#if this.brand}} 
    
            {{#if ../../subpage}}
                <h1>This shows now!</h1>
            {{/if}}
    
        {{/if}}  
    {{/each}}
    

    这应该与self.model 值类似:

    [
        {
            brand: "Brand A",
            otherFields: "here"
        }
    ]
    

    这是一个演示解决方案的 jsFiddle:nested if statement context escaping

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-01
      • 2017-03-29
      • 2016-11-26
      • 2012-09-04
      • 1970-01-01
      相关资源
      最近更新 更多