Syntax for foreach with break

Author Message

paul bolger

Friday 03 June 2011 3:44:46 pm

I need to run a foreach loop over an array of objects, test for class type (image) and then stop.... loop through until an image is found, output that image, stop looping.

It looks to me like foreach, break should do it. Does anybody have an example of the syntax required?

regards

paulb

Paul Bolger

Steven E. Bailey

Saturday 04 June 2011 3:23:05 pm

Not exactly what you are looking for but it does use a break:

{if $this_node.data_map.image.content.is_valid}
    {*has an image in the image attribute*}
    <img src={$this_node.data_map.image.content.['medium'].url|ezroot} />
{elseif ne($this_node.object.related_contentobject_count,0)}
    {foreach $this_node.object.related_contentobject_array as $related_object}
        {if eq($related_object.class_identifier,"image")}
            {*has an image as a related object*}
            <img src={$related_object.data_map.image.content.['medium'].url|ezroot} />
            {break}
       {/if}
    {/foreach}
{else}
    {*got bubkis*}
    <img src={"dummy.jpg"|ezimage}>
{/if}

Certified eZPublish developer
http://ez.no/certification/verify/396111

Available for ezpublish troubleshooting, hosting and custom extension development: http://www.leidentech.com

Jérôme Vieilledent

Sunday 05 June 2011 3:47:05 am

Hi Paul

You can use the {break} command for that, as suggested in Steven's snippet.

Take a look at the foreach reference doc for more info ;-).

paul bolger

Sunday 05 June 2011 5:01:47 am

Thanks Steven, That works well and was what I was looking for. Thanks too Jérôme. The documentation is a bit vague about how to use {break}, but now I understand. If only I could add a comment to the documentation so others could see what I've been shown... 

Paul Bolger

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 03:06:11
Script start
Timing: Jan 18 2025 03:06:11
Module start 'layout'
Timing: Jan 18 2025 03:06:11
Module start 'content'
Timing: Jan 18 2025 03:06:12
Module end 'content'
Timing: Jan 18 2025 03:06:12
Script end

Main resources:

Total runtime0.6947 sec
Peak memory usage4,096.0000 KB
Database Queries63

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0081 587.9141152.6250
Module start 'layout' 0.00810.0026 740.539139.4531
Module start 'content' 0.01080.6825 779.9922605.3281
Module end 'content' 0.69320.0014 1,385.320312.1641
Script end 0.6946  1,397.4844 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.4752160.0002
Check MTime0.00140.1991160.0001
Mysql Total
Database connection0.00200.288310.0020
Mysqli_queries0.639692.0708630.0102
Looping result0.00050.0739610.0000
Template Total0.661495.220.3307
Template load0.00190.272220.0009
Template processing0.659594.940820.3298
Template load and register function0.00010.010910.0001
states
state_id_array0.00160.225910.0016
state_identifier_array0.00170.240720.0008
Override
Cache load0.00160.2273220.0001
Sytem overhead
Fetch class attribute can translate value0.00050.068830.0002
Fetch class attribute name0.00090.126770.0001
XML
Image XML parsing0.00130.188430.0004
class_abstraction
Instantiating content class attribute0.00000.002280.0000
General
dbfile0.00100.1433300.0000
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
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/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
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
1content/datatype/view/ezxmltags/link.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/link.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 19
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs