Search code examples
phpsmarty

Smarty Template: printing 2 columns in every iteration


i have a php script that populates and assing my smarty array. i want to print two columns in one row then start another row.

  {section name=customer loop=$custid}

   <tr>

    // would love to start another loop here which will loop two times 
    <td>$custid<td>

    // and end loop here

    </tr>

   {/section}

Solution

  • You can use the mod operator with the current loop index, something like this (not tested) would probably work:

    {section name=customer loop=$custid}
        {assign var="column" value=$smarty.section.customer.index%2}
    
        {if $column==0}<tr>{/if}
        <td>$custid<td>
        {if $column==1 || $smarty.section.customer.last}</tr>{/if}
    {/section}