Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smarty - foreach loop 10 times and stop

Tags:

foreach

smarty

Im using the following Smarty code:

{foreach from=$entries key=i item=topic}
  {if $topic.topic_style == question}
    <li>
      <a href="topic.php?id={$topic.id}">{$topic.title}</a>
    </li>
  {/if}
{/foreach}

How can i do the {foreach} a maximum of 10 times and then stop?

like image 526
CLiown Avatar asked Aug 13 '10 07:08

CLiown


1 Answers

You can use index and break function:

{foreach from=$entries key=i item=topic name=foo}
  {if $smarty.foreach.foo.index == 10}
    {break}
  {/if}
  {if $topic.topic_style == question}
    <li>
      <a href="topic.php?id={$topic.id}">{$topic.title}</a>
    </li>
  {/if}
{/foreach}

Break function is described here:

Break in Smarty's / Dwoo's foreach

like image 96
hsz Avatar answered Oct 12 '22 00:10

hsz