multi-dimensional iteration in Smarty
Moderator: General Moderators
multi-dimensional iteration in Smarty
Is it possible to iterate through an array that is three or more levels deep with smarty? If so... how? My brain just isn't working real well tonight. 
I don't think you can iterate within an interation, however could be wrong..
Though as you are probably aware, you use associative arrays like so:
Just maybe you can nest the sections with something like:
untested and unsure.. not got anything to test on here.
Though as you are probably aware, you use associative arrays like so:
Code: Select all
{section name=i loop=$array}
<p>{$array[i].val1} {$array[i].val2}</p>
{/section}Code: Select all
{section name=i loop=$array}
{section name=j loop=$array[i]}
<p>{$array[i][j].val}</p>
{/section}
{/section}
I stilll can't seem to get it to work... maybe I'm going about it wrong. Say I have an array like:
I do not want to have to assign $Template->assign('title', $errors['title']) for every field... isn't there a way to traverse this with smarty?
Code: Select all
Array
(
[title] => Array
(
[0] => Must be between 1 and 100 characters in length.
[1] => Must contain only letters.
)
[body] => Array
(
[0] => Must be between 1 and 5000 characters in length.
[1] => Must contain the word "Chicken".
)
)- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I guess for that you would use:
Code: Select all
<p>{$title.body.0}</p>I've got this:
But it doesn't work...
This works, but it prints all the errors and not just the body errors:
Code: Select all
{foreach name=outer from=$form_errors item=errors}
{foreach from=$errors.body item=error}
<tr>
<td colspan="2" class="error">{$error}</td>
</tr>
{/foreach}
{/foreach}This works, but it prints all the errors and not just the body errors:
Code: Select all
{foreach name=outer from=$form_errors item=errors}
{foreach from=$errors item=error}
<tr>
<td colspan="2" class="error">{$error}</td>
</tr>
{/foreach}
{/foreach}-
AshrakTheWhite
- Forum Commoner
- Posts: 69
- Joined: Thu Feb 02, 2006 6:47 am
Code: Select all
arraykey1.arraykey2Code: Select all
$arraykey1['arraykey2']Code: Select all
{foreach name=outer from=$form_errors.body item=errors}
{foreach from=$errors item=error}
<tr>
<td colspan="2" class="error">{$error}</td>
</tr>
{/foreach}
{/foreach}- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Smarty or Template Lite has a really weird array syntax.
The following will work with multi-dimensional arrays.
While this will not work...
The element and element2 in the $myvar[element][element2] variable will be processed like Smarty/Template Lite constants.
The 'element' and 'element2' in the $myvar['element']['element2'] variable will be processed like text and not array elements. They don't quite follow the PHP convention for array variables inside a template.
If you want to be safe you should always use a period (.) between each element position in a multi-dimensional array instead of brackets ([]) when working inside templates.
You CAN use brackets ([]) like this $myvar[$element][$element2] in a multidimensional array in a template as long as the element inside the bracket is another variable. But like I said before it is best to use periods (.) to be safe.
The following will work with multi-dimensional arrays.
Code: Select all
{ $myvar.$element.$element2 }
{ $myvar[$element][$element2] }
{ $myvar.element.element2 }Code: Select all
{ $myvar[element][element2] }
{ $myvar['element']['element2'] }The 'element' and 'element2' in the $myvar['element']['element2'] variable will be processed like text and not array elements. They don't quite follow the PHP convention for array variables inside a template.
If you want to be safe you should always use a period (.) between each element position in a multi-dimensional array instead of brackets ([]) when working inside templates.
You CAN use brackets ([]) like this $myvar[$element][$element2] in a multidimensional array in a template as long as the element inside the bracket is another variable. But like I said before it is best to use periods (.) to be safe.