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

Accessing object properties within array of objects

 
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 11:33 pm    Post subject: Accessing object properties within array of objects Reply with quote

I'm a newbie and just beginning to run some tests on smarty. I can't seem to figure out the following situation.

I have an array of objects called 'Projects' and in each project I have 1 task object that contains the property 'date'. As a rule I do not access object properties except through a get method. ie to get the task object out of project I would use $project->get_task() which return the 'task' object, and same with the date ($task->get_date()).

My question is, how would loop through all the projects, list say the project title, then access the task object in the project and list the date property.

So really I'm just asking how to drill down through objects within the template.

I would like to know how to do this with a deeper number of arrays of objects too if possible. ie. list an array, with each object having another array of objects and those object having say 1 object in them and access a property from that bottom object.

Appreciate your help, just trying to work out if smarty is for me!

Thanks, Chris.
Back to top
View user's profile Send private message
chrisg101
Smarty Rookie


Joined: 25 Feb 2004
Posts: 7

PostPosted: Mon Mar 01, 2004 12:27 am    Post subject: Can anyone help me with this one? Reply with quote

Can anyone help me with this one?
Back to top
View user's profile Send private message
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon Mar 01, 2004 1:16 am    Post subject: Reply with quote

hmm. If I read you right, then this sounds vaguely like an approach I sometimes use.

In the following template sample, $BLOCK is an object assigned to the template and $BLOCK->blocks is an array of objects as in your example. The following loops that array and shows how properties can be accessed.
Code:
{foreach from=$BLOCK->blocks key=k item=block}
    <div class="{$block->style_class}" {$block->tag_options}>
        {include file=$block->template BLOCK=$block NAME=k}
    </div>
{/foreach}

In your case, it may look like this:
Code:
{foreach from=$projects item=project}
    {$project->get_task()}<br/>
    {$project->get_data()}<br/>
{/foreach}

HTH
Back to top
View user's profile Send private message
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