Traversing multi-dimensional arrays in Smarty
Tuesday, April 3rd, 2007 - 14:05We’ve been using Smarty templates for a few years now, and enjoy the separation of markup and application code that they allow, but the syntax does have a couple of odd sides to it that throw up issues now and then.
One of these issues is that multi-dimensional arrays are not handled properly by the ’section’ loop.
When redeveloping the reports, and adding the Per User page, we ran into this issue again. This report contains an array of Users. Each User also contains an array of Projects that have been worked on during the selected date range.
The ’section’ loop goes Bang when you try and traverse this, so we used the ‘foreach’ loop instead, as below :
{ foreach from=$userList item=user }
User Name
{ foreach from=$user.projects item=project }
Project info
{ /foreach }
{ /foreach }


