Smarty使用foreach

时间:2014-11-07 05:50:25

标签: smarty

    {foreach from=$last_comments item=lastcomment name=myLoop}

        <dt class="{if $smarty.foreach.myLoop.first}first_item{elseif $smarty.foreach.myLoop.last}last_item{else}item{/if}"><a href="{$lastcomment.link}" title="{$lastcomment.name|escape:html:'UTF-8'}">{$lastcomment.name|strip_tags|escape:html:'UTF-8'|truncate:25:'...'}</a></dt>
        <dd class="{if $smarty.foreach.myLoop.first}first_item{elseif $smarty.foreach.myLoop.last}last_item{else}item{/if}">
            <div class="star_content clearfix">
                {section name="i" start=0 loop=5 step=1}
                    {if $lastcomment.grade le $smarty.section.i.index}
                        <div class="star"></div>
                    {else}
                        <div class="star star_on"></div>
                    {/if}
                {/section}
            </div>
            {if $display_title && $lastcomment.title}{if $maxchar_title}{$lastcomment.title|escape:html:'UTF-8'|truncate:$maxchar_title:'...'}{else}{$lastcomment.title|escape:html:'UTF-8'}{/if}{elseif $display_title && $display_notitle}{l s='No title'  mod='blocklastcomments'}{/if}<p>{if $maxchar_comment}{$lastcomment.content|escape:html:'UTF-8'|truncate:$maxchar_comment:'...'}{else}{$lastcomment.content|escape:html:'UTF-8'}{/if}</p><span class="customer">{if $lastcomment.customer_name}({$lastcomment.customer_name|escape:html:'UTF-8'}.){/if}</span>
        </dd>
    {/foreach}

我想在固定的nr次迭代$ last_comments。我如何实现这一目标?

2 个答案:

答案 0 :(得分:0)

因为它可能是Smarty 2(Prestashop文档不清楚,我没有时间查看来源),你不能限制循环执行固定的次数,但你只能显示内容固定次数:

{foreach from=$last_comments item=lastcomment name=myLoop}
   {if $smarty.foreach.myLoop.iteration le 5}
     // rest of code put here
   {/if}
{/foreach}

以下带有if条件的循环将显示内部代码最多5次。

答案 1 :(得分:0)

由于prestashop 1.5适用于smarty 3,只需在满足条件时使用{break}

{if $lastcomment@iteration==5}
    {break}
{/if}