Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

looping through arrays of objects and more

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
chrisg101
Smarty Rookie


Joined: 25 Feb 2004
Posts: 7

PostPosted: Wed Feb 25, 2004 7:06 am    Post subject: looping through arrays of objects and more Reply with quote

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
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Wed Feb 25, 2004 9:09 am    Post subject: Reply with quote

Code:

{foreach from=$albums item=album}
  {$album->title}
  {foreach from=$album->songs item=song}
    {$song->title} / {$song->length}
  {/foreach}
{/foreach}
Back to top
View user's profile Send private message Send e-mail Visit poster's website
chrisg101
Smarty Rookie


Joined: 25 Feb 2004
Posts: 7

PostPosted: Wed Feb 25, 2004 10:08 pm    Post subject: assignning the object Reply with quote

Thanks for that. But do i need to assign the object to the template somehow?
Back to top
View user's profile Send private message
messju
Administrator


Joined: 16 Apr 2003
Posts: 3336
Location: Oldenburg, Germany

PostPosted: Thu Feb 26, 2004 1:27 am    Post subject: Reply with quote

yes, with assign_by_ref for example
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP