views:

431

answers:

1

I have a php page that produces an array of elements. For the sake of simplicity let's say that it contains the numbers 1-5 in numerical order. These numbers need to be equally (or as close to equal as possible) split into two columns (using a html table) like so:

1   4
2   5
3

The number of columns might change in the future. Since this is a change on the presentation level I assume that it should be something that can be achieved by making changes exclusively in the template file. Which suggests to me that it's Smarty that should handle the division of elements into columns.

Is there a way to achieve this with Smarty (and how) or should I let the php file do all the work?

+3  A: 

It is possible to do it in smarty, but since HTML and CSS don't really have support for columns it's a pain to do. It involves figuring out how many items there are in the array and using the {section} tags firstly to grab the first half (rounded up) of items, and display them in one column, and then a separate {section} to grab the second half (rounded up) of items and place them in the second column.

For {section} you can specify which array index to start at and how many items you want. If you set the number of items in the array as another variable, you can just calculate this.

You could try code like this - but I'm afraid I haven't tested those calculations so it may be wrong...

<td>
  {section name=myitem loop=$items max=(($itemcount/2)+0.499)}
    {$items[myitem]}
  {/section}
</td>
<td>
  {section name=myitem loop=$items start=(($itemcount/2)+0.499)}
    {$items[myitem]}
  {/section}
</td>
thomasrutter