Forums / Developer / Loop through items in object

Loop through items in object

Author Message

Pål Jostein Didriksen

Wednesday 28 June 2006 4:40:07 am

I have created an extension module, that reads data from a non-ez-table, and returns it as an object.

Now I would like to loop through the contents of the object and output both name (key) of each field and its contents it in the template, with something like this:

{foreach $myObject as $object}
    {$:index} : {$:key} : {$:value}
{/foreach}

I have however not been clever enough to achieve this. I can easily access individual values like this: {$:item.first_name}.

the contents of $myObject looks like this:

Array
(
    [0] => mymodule Object
        (
            [PersistentDataDirty] => 
            [id] => 1
            [contentobject_id] => 
            [remote_id] => 1574
            [first_name] => MyName
            [last_name] => LastName
            [address1] => Drammensveien 1
            [address2] => 
            [address3] => 
            [postal_code] => 10
            [postal_addr] => Oslo

        )

)

Any tips?

Łukasz Serwatka

Monday 03 July 2006 11:30:09 pm

First you need to assing value to the TPL variable $myObject. In TPL define variable then in PHP use

$tpl->setVariable( 'myObject', $myObject );

Then you need to use:

{foreach $myObject[0] as $index => $value} 
   {$index} : {$value}
{/foreach}

or just

$tpl->setVariable( 'myObject', $myObject[0] );

then

{foreach $myObject as $index => $value} 
   {$index} : {$value}
{/foreach}

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog