Forums / Developer / Appending an array with a concatenated string of variables

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' ).

eZ debug

Timing: Jan 18 2025 03:06:03
Script start
Timing: Jan 18 2025 03:06:03
Module start 'content'
Timing: Jan 18 2025 03:06:03
Module end 'content'
Timing: Jan 18 2025 03:06:03
Script end

Main resources:

Total runtime0.1562 sec
Peak memory usage4,096.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0057 587.9688180.8047
Module start 'content' 0.00570.0045 768.773494.2109
Module end 'content' 0.01020.1460 862.9844523.9063
Script end 0.1562  1,386.8906 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00332.1282200.0002
Check MTime0.00130.8426200.0001
Mysql Total
Database connection0.00080.500010.0008
Mysqli_queries0.110870.95051410.0008
Looping result0.00130.84101390.0000
Template Total0.145793.310.1457
Template load0.00070.462110.0007
Template processing0.144992.789310.1449
Override
Cache load0.00050.329110.0005
Sytem overhead
Fetch class attribute can translate value0.00080.540910.0008
XML
Image XML parsing0.00020.151110.0002
General
dbfile0.00543.4832200.0003
String conversion0.00000.003130.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs