|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
chrisg101 Smarty Rookie
Joined: 25 Feb 2004 Posts: 7
|
Posted: Wed Feb 25, 2004 7:06 am Post subject: looping through arrays of objects and more |
|
|
Hi,
I am just having a look at smarty, and I want to check one thing before I go implementing into my current code (I don't currently use a template system, but hoping to use one soon).
I've looked at the code for pulling properties out of objects. But what about the following situation:
All my code is based around objects, and some of these objects have arrays of objects themselves. Often I will Want to loop through both.
Say an array called $albums consists of a bunch of 'Album' objects, and an album contains the property $songs, which is an array of 'Song'objects.
How would I loop through both Albums, and loop through the songs in each album displaying each album and song in some sort of table format? Is there an example around or could some one give me a bit of an example. So far been looking around and haven't been able to find anything.
Appreciate code.
Regards, Chris. |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Wed Feb 25, 2004 9:09 am Post subject: |
|
|
Code: |
{foreach from=$albums item=album}
{$album->title}
{foreach from=$album->songs item=song}
{$song->title} / {$song->length}
{/foreach}
{/foreach}
|
|
|
Back to top |
|
chrisg101 Smarty Rookie
Joined: 25 Feb 2004 Posts: 7
|
Posted: Wed Feb 25, 2004 10:08 pm Post subject: assignning the object |
|
|
Thanks for that. But do i need to assign the object to the template somehow? |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Thu Feb 26, 2004 1:27 am Post subject: |
|
|
yes, with assign_by_ref for example |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|