Test for empty array

Author Message

paul bolger

Wednesday 31 October 2007 7:12:26 pm

Is there a neat way to test if an array is empty?

I'm trying to make a heading not appear if an array of fetched items doesn't contain any items.

I realise that you could run a foreach loop and then do a has_content test on the first item, but this seems clumsy - is this the only way?

Paul Bolger

Łukasz Serwatka

Wednesday 31 October 2007 11:27:46 pm

Hi Paul,

You can test it with count() tpl operator

{* Example 1 *}
{def $my_array=array()}

{if $my_array|count()}

{else}

{/if}

{* Example 2 *}

{if $my_array|count()|gt(0)}

{else}

{/if}

Hope that helps.

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

André R.

Thursday 01 November 2007 2:33:30 am

Doesn't empty arrays evaluate to false ??
like this:

{if $my_array}
{else}
{/if}

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

paul bolger

Thursday 01 November 2007 4:07:21 pm

Thanks, both work.

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 19:24:47
Script start
Timing: Jan 18 2025 19:24:47
Module start 'layout'
Timing: Jan 18 2025 19:24:47
Module start 'content'
Timing: Jan 18 2025 19:24:49
Module end 'content'
Timing: Jan 18 2025 19:24:49
Script end

Main resources:

Total runtime1.4622 sec
Peak memory usage4,096.0000 KB
Database Queries62

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0051 589.0234152.6250
Module start 'layout' 0.00510.0021 741.648439.4453
Module start 'content' 0.00721.4535 781.0938599.5938
Module end 'content' 1.46070.0015 1,380.687512.1719
Script end 1.4622  1,392.8594 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.2151160.0002
Check MTime0.00130.0918160.0001
Mysql Total
Database connection0.00080.052010.0008
Mysqli_queries1.409196.3676620.0227
Looping result0.00060.0385600.0000
Template Total1.430397.820.7151
Template load0.00220.150620.0011
Template processing1.428097.662920.7140
Template load and register function0.00010.007410.0001
states
state_id_array0.00080.057910.0008
state_identifier_array0.00130.089820.0007
Override
Cache load0.00190.1276210.0001
Sytem overhead
Fetch class attribute can translate value0.00070.048130.0002
Fetch class attribute name0.00130.088070.0002
XML
Image XML parsing0.00170.117430.0006
class_abstraction
Instantiating content class attribute0.00000.000980.0000
General
dbfile0.00100.0676310.0000
String conversion0.00000.000440.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
5content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
2content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 18
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs