Forums / Developer / Syntax for foreach with break

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