问题
{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}
I would like to iterate through the $last_comments a fixed nr of times. How do I achieve this?
回答1:
As prestashop 1.5 works with smarty 3, just use {break} when the condition is met
{if $lastcomment@iteration==5}
{break}
{/if}
回答2:
Because it's probably Smarty 2 (Prestashop documentation is unclear and I don't have time to look into sources), you cannot limit loop to be executed fixed number of times but you can display content only a fixed number of times:
{foreach from=$last_comments item=lastcomment name=myLoop}
{if $smarty.foreach.myLoop.iteration le 5}
// rest of code put here
{/if}
{/foreach}
The following loop with if condition will display inner code maximum 5 times.
来源:https://stackoverflow.com/questions/26794921/smarty-using-foreach