How to assign a value to a single array element?

Author Message

E Hoelzler

Saturday 30 December 2006 3:21:04 am

The following code shows, how I tried to modify single elements of an array and the result.

{def $myarray=array(5,7,9)}
myarray = {$myarray[0]}, {$myarray[1]}, {$myarray[2]}<br>
or: {$myarray.0}, {$myarray.1}, {$myarray.2}<br>
loop display:<br>
{foreach $myarray as $ma}
  myarray-element: {$ma}<br>
{/foreach}
{set $myarray.0=20}  {* WHY DOESNT IT WORK?*}
now the array is destroyed<br>
myarray = {$myarray[0]}, {$myarray[1]}, {$myarray[2]}<br>
{foreach $myarray as $ma}
  myarray-element: {$ma}<br>
{/foreach}
{* result not better using an associative array*}
{def $myassocarray=hash('first', 'a',
			'second', 'b',
			'third', 'c')}

myassocarray = {$myassocarray.first}, {$myassocarray.second}, {$myassocarray.third}<br>
myassocarray(alt) = {$myassocarray[first]}, {$myassocarray[second]}, {$myassocarray[third]}<br>
{set $myassocarray.first='x'}

myassocarray(changed) = {$myassocarray.first}, {$myassocarray.second}, {$myassocarray.third}

The result was:

myarray = 5, 7, 9
or: 5, 7, 9
loop display:
myarray-element: 5
myarray-element: 7
myarray-element: 9

now the array is destroyed
myarray = , , 
myassocarray = a, b, c
myassocarray(alt) = a, b, c
myassocarray(changed) = , , 

Claudia Kosny

Tuesday 02 January 2007 2:11:58 pm

Hi E

As you most likely have found out by now assigning a new value to an array element is not possible directly. This fact is hidden quite well in the docu:

The "set" function can not be used to change the value of an element/attribute of an array, hash or an object. In fact, the elements/attributes of arrays, hashes and objects can not be changed from within template code.

http://ez.no/doc/ez_publish/technical_manual/3_8/templates/the_template_language/variable_usage

So best write yourself a custom template operator that does the job, it is really easy to do. First check the contribution section though, maybe there is already such an operator there.

Claudia

kracker (the)

Tuesday 02 January 2007 10:14:10 pm

interesting, it might take a person a long time to clearly see that end. that's worth documenting more clearly.

//kracker

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

E Hoelzler

Wednesday 03 January 2007 12:04:04 pm

Hi Claudia, thank you for your hint - Indeed - I have read that part of the documentation several times. And I didn't recognize those tiny little sentences. And kracker is right: It took me quite a time to get to this point.
What I ACTUALLY wanted to do, I will realize now using a custom event.

Thanks for help

Erwin

Betsy Gamrat

Wednesday 24 January 2007 5:34:25 pm

http://ez.no/doc/ez_publish/technical_manual/3_8/reference/template_operators/arrays/insert

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 22:38:54
Script start
Timing: Jan 18 2025 22:38:55
Module start 'layout'
Timing: Jan 18 2025 22:38:55
Module start 'content'
Timing: Jan 18 2025 22:38:55
Module end 'content'
Timing: Jan 18 2025 22:38:55
Script end

Main resources:

Total runtime0.0198 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0074 588.0469152.6406
Module start 'layout' 0.00740.0040 740.687539.4922
Module start 'content' 0.01140.0060 780.179793.4922
Module end 'content' 0.01740.0023 873.671934.3047
Script end 0.0197  907.9766 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.003015.0318140.0002
Check MTime0.00136.4544140.0001
Mysql Total
Database connection0.00115.557510.0011
Mysqli_queries0.003819.177630.0013
Looping result0.00000.112110.0000
Template Total0.00189.110.0018
Template load0.00115.557510.0011
Template processing0.00073.559910.0007
Override
Cache load0.00083.948110.0008
General
dbfile0.00031.619080.0000
String conversion0.00000.055540.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_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