Smarty - foreach loop 10 times and stop

后端 未结 8 2299
执念已碎
执念已碎 2021-02-07 12:31

Im using the following Smarty code:

{foreach from=$entries key=i item=topic}
  {if $topic.topic_style == question}
    
  • 相关标签:
    8条回答
    • 2021-02-07 13:08

      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

      0 讨论(0)
    • 2021-02-07 13:10

      You could just use array_slice:

      {foreach $entries|@array_slice:0:10 as $topic}
        ...
      {/foreach}
      
      0 讨论(0)
    提交回复
    热议问题