聪明的foreach最后不能正常工作?

时间:2012-04-10 23:21:13

标签: php html smarty

我在为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”但中间结果如第3个结果?

<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>

1 个答案:

答案 0 :(得分: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}
      …