Search code examples
htmlhtml-tablesmarty

Smarty: dynamic colspan


I have a smarty template where I want to make a dynamic colspan (one column colspawned through the full foreach. For example if foreach has only one element it's just one cell, but if it has 100 element it's a colspan through the 100 element)

My code so far, but it's not dynamic yet:

<table>
{foreach $myvar as $myvar}
  <tr>
   <td>{$myvar.text}</td>
   <td{if $myvar.isTrue == 1} style="background-color: green;"
 {/if}>
   {if $myvar.isSelected}<i class="fa fa-user"></i>{/if}
   </td>
   <td colspan=@count($myvar)>{$myvar.colname}</td>
  </tr>
 {/foreach}
</table>

How to be dynamic?


Solution

  • In Smarty to count array's length you would use

    {$myArray|@count}
    

    Your code should look something like this:

    <table>
    {foreach $myArray as $myVar}
      <tr>
       <td>
          {$myVar.text}
       </td>
       <td
          {if $myVar.isTrue == 1} style="background-color: green;"{/if}>
          {if $myVar.isSelected}<i class="fa fa-user"></i>{/if}
       </td>
       <td colspan="{$myArray|@count}">
          {$myVar.colname}
       </td>
      </tr>
     {/foreach}
    </table>