RE: [SMARTY] Loops
| From: | Erich Beyrent | Date: | Fri, 12 Mar 2004 17:14:30 +0000 |
| Subject: | RE: [SMARTY] Loops | ||
| References: | 1 | Groups: | php.smarty.general |
| Request: | Send a blank email to php-smarty-general+get-11104@lists.php.net to get a copy of this message | ||
> Hi all,
>
> I don't know why I have so much trouble with Smarty and arrays... Let's
> assume I have the following data structure:
>
> $EVENTS = array('event1' => array('dow' => "Saturday",
> 'month' => "March",
> 'day' => "13",
> 'year' => "2004",
> 'time' => "6:00 PM",
> 'title' => "St. Patrick\'s
> Day Supper",
> 'location' => "Fellowship
> Hall",
> ),
> 'event2' => array('dow' => "Saturday",
> 'month' => "March",
> 'day' => "27",
> 'year' => "2004",
> 'time' => "7:30 PM",
> 'title' => "Michael Nix
> Concert",
> 'location' => "Sanctuary",
> ),
> );
>
> How would I loop through all of the events in my template?
>
> I tried this:
>
> // Loop through each event
> {section name=i loop=$Events}
> // Loop through the values for each event
> {section name=j loop=$Events[i]}
> {$Events[i][j].title}<br>
> {/section}
> {foreach key=i item=event from=$Events}
> {$event.title} or {$Events.$i.title}
> {/foreach}
>
> section only works if your array-keys are numeric and from 0..n-1 .
I see, he says. Is it more advantageous for me to simply change the data
structure to this:
$EVENTS = array(array('dow' => "Saturday",
'month' => "March",
'day' => "13",
'year' => "2004",
'time' => "6:00 PM",
'title' => "St. Patrick\'s Day Supper",
'location' => "Fellowship Hall",
),
array('dow' => "Saturday",
'month' => "March",
'day' => "27",
'year' => "2004",
'time' => "7:30 PM",
'title' => "Michael Nix Concert",
'location' => "Sanctuary",
),
);
And then loop through it with
{section name=i loop=$Events}
{$Events[i].title}
{/section}
Are there benefits of doing it one way or the other?
-Erich-