Appending an array with a concatenated string of variables

Author Message

Mark Yasuda

Tuesday 12 April 2011 8:44:29 am

Hi there,

I'm new to eZ Publish, but well versed in PHP.
I'm currently working in a template file and struggling somewhat with the syntax / rules, I wonder if someone might be able to help?

I have defined an empty array;

{def $names = array()}

at the top of my particular template file.
I then have a bunch of stuff outputting in HTML, and finally a foreach loop as follows;

{foreach $some_objects as $index => $rel_objects }
  {if eq( $rel_objects.class_identifier, 'speaker' )}
    {$names|append( concat( $rel_objects.data_map.first_name.content , ' ' , $rel_objects.data_map.last_name.content ) ) }
  {/if}
 {/foreach}
 {if gt( $names|count() , 0 )}
  {$names|implode( ', ' )}
 {/if}

My problem seems to be that the concatenation is incorrectly done, as I end up echoing $names as "Array Array Array ..." etc... (note, without any seperating commas!?...)

Any help would be appreciated here, I have no idea why the $rel_objects.data_map.first_name.content is being echo'd as an array when;

{$rel_objects.data_map.first_name.content}

outputs a string (as expected).

Thanks!

André R.

Tuesday 12 April 2011 1:33:42 pm

append does not change the array, it generates a new array. So you need to use se:

{set $names = $names|append(....)}

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

Mark Yasuda

Wednesday 13 April 2011 1:27:27 am

That's great, thanks André!
The documentation seems a little confusing regarding this, I'll add a comment to it so hopefully others may benefit.

Thanks again,

Mark

edit: seems like documentation comments were locked down due to spamming.
oh well... maybe someone from eZ docs team will see this and expand on the array append page to reflect that the array does not modify the original input array. Perhaps another example as below:

Example 3

{set $foo = array( 1 , 2 , 3 )}
{set $bar = array( 'red' , 'blue' , 'green' )}
{set $result = $foo|append( $bar )}

Array $result contains: ( 1, 2, 3, 'red', 'blue', 'green' ).

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 05:00:13
Script start
Timing: Jan 18 2025 05:00:13
Module start 'layout'
Timing: Jan 18 2025 05:00:13
Module start 'content'
Timing: Jan 18 2025 05:00:14
Module end 'content'
Timing: Jan 18 2025 05:00:14
Script end

Main resources:

Total runtime1.0876 sec
Peak memory usage4,096.0000 KB
Database Queries57

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0062 588.1719152.6563
Module start 'layout' 0.00620.0034 740.828139.5156
Module start 'content' 0.00961.0766 780.3438545.2109
Module end 'content' 1.08620.0014 1,325.554712.0938
Script end 1.0876  1,337.6484 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00330.3024160.0002
Check MTime0.00140.1242160.0001
Mysql Total
Database connection0.00060.055610.0006
Mysqli_queries1.032794.9468570.0181
Looping result0.00110.0981550.0000
Template Total1.046796.220.5234
Template load0.00210.192820.0010
Template processing1.044696.042320.5223
Template load and register function0.00020.015210.0002
states
state_id_array0.00170.160610.0017
state_identifier_array0.00190.174120.0009
Override
Cache load0.00180.1651310.0001
Sytem overhead
Fetch class attribute can translate value0.00050.050220.0003
Fetch class attribute name0.00100.094540.0003
XML
Image XML parsing0.00060.052920.0003
class_abstraction
Instantiating content class attribute0.00000.000840.0000
General
dbfile0.00250.2266170.0001
String conversion0.00000.000940.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
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
10content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.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
1content/datatype/view/ezxmltags/strong.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/strong.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 26
 Number of unique templates used: 8

Time used to render debug report: 0.0001 secs