advanced use of foreach in smarty  Bottom

  • I didn't know that one, so i give it you ...

    I needed a way to display a separator after all link in an extmenu except the last one....

    ... and you can do it in a quite simple way with Smarty
    indeed

    sample use

    Code

    <ul>
    <!--[foreach from=$items key='myId' item='item' name='foo']-->
      <li>
      <!--[if $smarty.foreach.foo.first]--> First <!--[/if]-->
        <!--[$item.label]-->
      <!--[if $smarty.foreach.foo.last]--> Last<!--[/if]-->
      </li>
    <!--[/foreach]-->
    </ul>


    http://www.smarty.net/manual/en/language.function.foreach.php
  • Also useful for comma separated lists:

    Code

    <ul>
    <!--[foreach from=$items key='key' item='item' name='foo']-->
      <li>
        <!--[$item.label]-->
        <!--[if $smarty.foreach.foo.last neq true]-->, <!--[/if]-->
      </li>
    <!--[/foreach]-->
    </ul>


    or a CSS class handling of the first and last elements:

    Code

    <ul>
    <!--[foreach from=$items key='key' item='item' name='foo']-->
      <!--[assign var='cssClass' value='']-->
      <!--[if $smarty.foreach.foo.first neq true]-->
        <!--[assign var='cssClass' value='first']-->
      <!--[elseif $smarty.foreach.foo.last neq true]-->
        <!--[assign var='cssClass' value='last']-->
      <!--[/if]-->

      <li<!--[if $cssClass neq '']--> class="<!--[$cssClass]-->"<!--[/if]-->>
        <!--[$item.label]-->
      </li>
    <!--[/foreach]-->
    </ul>


    Thanks mumuri for enlighten this cool stuff icon_wink

    --
    - Mateo T. -
    Mis principios... son mis fines

This list is based on users active over the last 60 minutes.