Home > Archive > PHP Smarty Templates > March 2004 > Re: [SMARTY] Loops
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: [SMARTY] Loops
|
|
| Messju Mohr 2004-03-19, 1:29 pm |
| On Fri, Mar 12, 2004 at 11:36:39AM -0500, Erich Beyrent wrote:
> 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 .
> Which didn't work. Tips? Suggestions? As always, thanks in advance!
>
> -Erich-
| |
| Erich Beyrent 2004-03-19, 1:29 pm |
| > 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-
| |
| Mark Rogers 2004-03-19, 1:29 pm |
| Erich Beyrent writes:
> I see, he says. Is it more advantageous for me to simply change the data
> structure to this [...]
Each to his own; I don't think I ever use section, as I find having
meaningful array keys useful, and even when they are simply numerical
foreach copes just as well as section. Indeed, having used Smarty for some
time I couldn;t tell you the syntax for section without looking it up.
On the other hand, I suspect that section is more commonly used (from the
comments and sample code I've seen).
If you typically have meaningful array keys, use foreach. If you don't, use
section.
To me,
{foreach from=$Events item=event}
{$event.title}
{/foreach}
... just "makes sense", even for numerical keys.
--
Mark Rogers,
More Solutions Ltd :: Tel: 0845 45 89 555
|
|
|
|
|