Smarty: sections and iterators

Just wanted to point this out to any who may be interested, or can’t seem to figure out how to hop around array indices in the Smarty template.

In straight PHP it’s simple enough to bounce around to different array indices $array[$ii-1] etc. In Smarty, not so easy.

If you want to compare the current array member’s value to the one previous, you have to use a special modifier/keyword in your template: index_previous. Likewise you’d use index_next for the next index.

{section name=ii loop=$people}
{$people[ii].name} is {$people[ii].age}
{if $people[ii.index_prev].age < $people[ii].age}
{$people[ii].name} is older than {$people[ii.index_prev].name}
{/if}
{/section}

You can find more info on Smarty sections HERE.

Leave a comment

Hey there! Come check out all-new content at my new mistercameron.com!