Smarty - foreach loop 4 times and create a new list

Posted by Harold on Stack Overflow See other posts from Stack Overflow or by Harold
Published on 2012-06-17T08:54:38Z Indexed on 2012/06/17 9:16 UTC
Read the original article Hit count: 227

Filed under:
|
|

I want to achieve a list like this with smarty.

<ul>
 <li>
   <a>img1</a>
   <a>img2</a>
   <a>img3</a>
   <a>img4</a>
 </li>
 <li>
   <a>img5</a>
   <a>img6</a>
   <a>img7</a>
   <a>img8</a>
 </li>
<li>
   <a>img9</a>
   <a>img10</a>
   <a>img11</a>
   <a>img12</a>
 </li>
</ul>

Using this sample code

<ul class="bullet">
    {foreach from=$manufacturers item=manufacturer name=manufacturer_list}
        {if $smarty.foreach.manufacturer_list.index < 4}
      <li class="{if $smarty.foreach.manufacturer_list.last}last_item{elseif $smarty.foreach.manufacturer_list.first}first_item{else}item{/if}">   

        <a href="{$link->getmanufacturerLink($manufacturer.id_manufacturer, $manufacturer.link_rewrite)}" title="{l s='More about' mod='blockmanufacturer'}{$manufacturer.name}"> <img src="{$img_manu_dir}{$manufacturer.id_manufacturer}.jpg"><span>{$manufacturer.name}<span></a>
      </li>
    {/if}
{/foreach}

First with given array $manufacturers it will loop inside a <li> max of 4 times and will create 4 <img>. And then, when it reach the 4th index, it will make a new <li> tag.

Thanks for the help!

© Stack Overflow or respective owner

Related posts about php

Related posts about html