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

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.

eZ debug

Timing: Jan 18 2025 19:05:28
Script start
Timing: Jan 18 2025 19:05:28
Module start 'layout'
Timing: Jan 18 2025 19:05:28
Module start 'content'
Timing: Jan 18 2025 19:05:29
Module end 'content'
Timing: Jan 18 2025 19:05:29
Script end

Main resources:

Total runtime0.5910 sec
Peak memory usage4,096.0000 KB
Database Queries54

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 589.0469152.6250
Module start 'layout' 0.00600.0029 741.671939.4453
Module start 'content' 0.00890.5807 781.1172530.2422
Module end 'content' 0.58960.0014 1,311.35948.1641
Script end 0.5910  1,319.5234 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.5536160.0002
Check MTime0.00130.2191160.0001
Mysql Total
Database connection0.00140.240110.0014
Mysqli_queries0.542391.7606540.0100
Looping result0.00050.0835520.0000
Template Total0.555694.020.2778
Template load0.00200.336220.0010
Template processing0.553693.671320.2768
Template load and register function0.00010.020810.0001
states
state_id_array0.00130.221310.0013
state_identifier_array0.00170.289820.0009
Override
Cache load0.00170.2836200.0001
Sytem overhead
Fetch class attribute can translate value0.00150.247520.0007
Fetch class attribute name0.00100.168030.0003
XML
Image XML parsing0.00080.135720.0004
class_abstraction
Instantiating content class attribute0.00000.001330.0000
General
dbfile0.00180.3013180.0001
String conversion0.00000.001240.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
2content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 16
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs