-1

我在从foreach的最后一个选项中添加一个类时遇到了麻烦,它出于某种原因重复它有什么想法吗?

{if $subcategories} 
<div class="subcategories">
{if $subcategories|@count < 6}
    <ul>
{/if}
{foreach from=$splitted_subcategories item="ssubcateg"}
    {if $subcategories|count >= 6}
        <div class="subcategories">
            <ul>
    {/if}
        {foreach from=$ssubcateg item=category name="ssubcateg"}
        {if $category.category_id}<li{if $smarty.foreach.ssubcateg.last} class="last"{/if}><a href="{"categories.view?category_id=`$category.category_id`"|fn_url}" class="strong">{$category.category}</a></li>{/if}

    {/foreach}
    {if $subcategories|count >= 6}
            </ul>
        </div>
    {/if}
{/foreach}
{if $subcategories|count < 6}
</ul>
{/if}
</div>
{/if}

输出:似乎在重复?如果有 6 个结果是添加一个 class="last" 但中间结果如第三个结果?

<ul> 
<li class="last"><a class="strong" href="#">Link</a></li>
    <li><a class="strong" href="#">Link</a></li>
    <li class="last"><a class="strong" href="#">Link</a></li>
    <li><a class="strong" href="#">Link</a></li>
</ul>
4

1 回答 1

0

您的最后一项正在重复,因为您正在检查“在内部循环中处于最后”。您忽略了内部循环本身是循环的。尝试:

{foreach from=$splitted_subcategories item="ssubcateg" name="outer"}
  {foreach from=$ssubcateg item=category name="ssubcateg"}
    {if $smarty.foreach.outer.last && $smarty.foreach.ssubcateg.last}
      …
于 2012-04-11T08:15:48.960 回答