multi-dimensional iteration in Smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

multi-dimensional iteration in Smarty

Post by Luke »

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. :(
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

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:

Code: Select all

{section name=i loop=$array}
    <p>{$array[i].val1} {$array[i].val2}</p>
{/section}
Just maybe you can nest the sections with something like:

Code: Select all

{section name=i loop=$array}
  {section name=j loop=$array[i]}
    <p>{$array[i][j].val}</p>
  {/section}
{/section}
untested and unsure.. not got anything to test on here.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I stilll can't seem to get it to work... maybe I'm going about it wrong. Say I have an array like:

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".
        )

)
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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, you can. You can foreach or section the arrays (or switch in between the two). Try it, it ispretty easy to do and pretty cool.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I guess for that you would use:

Code: Select all

<p>{$title.body.0}</p>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I've got this:

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}
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 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

Post by AshrakTheWhite »

Code: Select all

arraykey1.arraykey2
in smarty means the same as

Code: Select all

$arraykey1['arraykey2']
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

then how come the first example doesn't work? :(
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:oops: Nevermind I figured it out

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}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Way to go Ninja.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Smarty or Template Lite has a really weird array syntax.

The following will work with multi-dimensional arrays.

Code: Select all

{ $myvar.$element.$element2 }

{ $myvar[$element][$element2] }

{ $myvar.element.element2 }
While this will not work...

Code: Select all

{ $myvar[element][element2] }

{ $myvar['element']['element2'] }
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.
Post Reply