|
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 11:33 pm Post subject: Accessing object properties within array of objects |
|
|
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 |
|
chrisg101 Smarty Rookie
Joined: 25 Feb 2004 Posts: 7
|
Posted: Mon Mar 01, 2004 12:27 am Post subject: Can anyone help me with this one? |
|
|
Can anyone help me with this one? |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Mon Mar 01, 2004 1:16 am Post subject: |
|
|
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 |
|
|
|
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
|