Page 1 of 1

Simplifying complex {if}{else} statements in {foreach} loops

Posted: Mon Aug 25, 2014 10:22 am
by psy
An easy way to exclude items is to put one {if} conditional statement, an action and close the {/if} at the beginning of the loop, eg:

Code: Select all

{foreach from=$items item='entry'}
{if $entry == 'something' || or $entry < 15} 
  {continue}
{/if}
<p>do whatever with all the other entries.</p>
{/foreach}
Then when the loop encounters an entry that meets the condition, it immediately moves on to the next entry.

You can also shorten {foreach} processing times by making the loop stop once it meets the first instance of a condition, eg:

Code: Select all

{foreach from=$items item='entry'}
{if $entry == 'something' }
  <p>Hello {$entry}</p>
  {break} 
{/if}
{/foreach}