2016-07-16 118 views
0

工作,我在树枝模板有这样的建筑IF语句没有在另一个IF语句中的树枝

{% for category in categories %} 
        {% if category.parentId == 0 %} 
         {% set parent = category.id %} 
         <li class="menu-item dropdown"> 
          <a class="dropdown-toggle" data-toggle="dropdown" href="#"> 
           {{ category.name }} 
           <span class="caret"></span> 
          </a> 
          <ul class="dropdown-menu"> 
           {% if category.parentId == parent %} 
            <li><a href="#">{{ category.id }}</a></li> 
           {% endif %} 
          </ul> 
         </li> 
        {% endif %} 
       {% endfor %} 

我的问题是在这,那第二个条件IF(如果category.parentId ==父母)没有按没有工作,所以,我无法获得子类别列表。

有谁知道,那里有什么问题,我该如何解决?

感谢

回答

1

如果在树枝语句工作正常,但你必须有误。您将category.id与category.parentId在同一个对象中进行比较。你必须有另一个foreach循环的子类别。像这样:

{% for category in categories %} 
       {% if category.parentId == 0 %} 
        {% set parent = category.id %} 
        <li class="menu-item dropdown"> 
         <a class="dropdown-toggle" data-toggle="dropdown" href="#"> 
          {{ category.name }} 
          <span class="caret"></span> 
         </a> 
         <ul class="dropdown-menu"> 
          {% for subCategory in categories %} 
          {% if subCategory.parentId == parent %} 
           <li><a href="#">{{ subCategory.id }}</a></li> 
          {% endif %} 
          {% endfor %} 
         </ul> 
        </li> 
       {% endif %} 
      {% endfor %} 
+0

哇,非常感谢您的帮助,它的工作正常:) –

+0

欢迎:) – Nikdyvice