Array append Problem

Author Message

Michael Hoenig

Thursday 25 March 2004 3:08:43 pm

Hi, I'm new in ezPublish and I try to append a content of a variable to an existing array with the following code:

{let ort_array=array()}
...
{section loop=$node.object.related_contentobject_array}
...
{$ort_array|append($:item)}
{/section}
...
{/let}

Thanks for your help!!

Michael

Rinze Cats

Wednesday 09 June 2004 6:03:42 am

I am having troubles dymanically adding items in an array using a loop as well. Can anybody shine a light on the above problem?

Hugo Siles

Wednesday 09 June 2004 6:13:12 am

hi, you should try something like this

{let ort_array=array()}
...
{section loop=$node.object.related_contentobject_array}
...
{set ort_array=$ort_array|append($:item)}
{/section}
...
{/let}

Rinze Cats

Wednesday 09 June 2004 6:53:03 am

Hmm,

i was using the correct code, but it isn't working because I use a name for my section. Maybe you can help me with this code

{let my_array=array()}
{section loop=$current_node.object.related_contentobject_array}
{set my_array=$my_array|append($:item.data_map.positie.content.0)}
{/section}
{$my_array|count}
{/let}

this works, and count outputs 20. Adding the name (omitting it isn't an option right now)

{let my_array=array()}
{section name=Container loop=$current_node.object.related_contentobject_array}
{set my_array=$my_array|append($:item.data_map.positie.content.0)}
{/section}
{$my_array|count}
{/let}

This code, certainly is incorrect, but I can't get it to work (tried a godzillion syntatical variations ;-( )
greetz
rinze

Hugo Siles

Wednesday 09 June 2004 7:11:30 am

hi, again

{let my_array=array()}
{section name=Container loop=$current_node.object.related_contentobject_array}
{set my_array=$my_array|append(<b>$Container</b>:item.data_map.positie.content.0)}
{/section}
{$my_array|count}
{/let}

also it would be a good idea to look at this pages
http://ez.no/ez_publish/documentation/development/libraries/ez_template/functions/section
or
http://ez.no/ez_publish/documentation/toc

regards

hugo

Rinze Cats

Wednesday 09 June 2004 7:38:13 am

are you certain this code is correct? I have tried this, and tried it again just now (copy-paste), but it doesn't seem to work. Count results in 0. I am using 3.3.5.

the following error is produced by the debugger.

Warning: eZTemplate:set Jun 09 2004 16:34:30 
Variable 'Container:my_array' doesn't exist, cannot set

I have previously tried to solve this by adding the container prefix on an trial and error basis, resulting in some marvelous errors, but never the count of 20 ;-(

Hugo Siles

Wednesday 09 June 2004 7:48:14 am

hi, you are right, i forgot one important thing, as long as the my_array is in another namespace you cannot modify it in the Container namespace

in this page this problem is explained.. try it
http://ez.no/ez_publish/documentation/development/libraries/ez_template/basics/namespaces_

i guess the best solution it's ommit the name "Container" in the {section} tag

Regards

Hugo

Paul Forsyth

Wednesday 09 June 2004 7:57:13 am

You could omit the namespace altogether and use the new var= construct, eg:

{let my_array=array()}
{section var=Container loop=$current_node.object.related_contentobject_array}
{set my_array=$my_array|append($Container.data_map.positie.content.0)}
{/section}
{$my_array|count}
{/let}

Notice the new use of Container. The var parameter does away with namespaces...

paul

--
http://www.visionwt.com

Rinze Cats

Thursday 10 June 2004 1:18:18 am

Hi all, and thanx for the help. I replaced the namespace tag with the var tag and replaced all the references to the namespace from within my section (posted code is just a sniplet).

All is well now! I find it strange though, that a global variable cannot be adressed from within another namespace.

greetz
rinze

Rinze Cats

Thursday 10 June 2004 5:01:11 am

Mm, runnig into some weird behaviour again, do any of you have an explanation for this?

The code should first initialize an array, then it should dynamically at items to this array, if the item doesn't already exist. The general idea is to fetch an objects position value, or assign the first available position number randomly, if the position is missing from the current object.
The second part of this code snipplet is behaving really weird

{let my_array=array()}
{section var=Container loop=$current_node.object.related_contentobject_array}
{section show=and(ne($Container.data_map.positie.content.0,''), ne($Container.data_map.positie.content.0, '0'))}
	{set my_array=$my_array|append($Container.data_map.positie.content.0)}
{/section}
{/section}

{section var=counter loop=20}
	{section show=$my_array|contains($counter)}
		{* do nothing *}
	{section-else}
		{set my_array=$my_array|append($counter)}
	{/section}
{/section}
{/let}

When I omit the section-else part, the function works. With the {section-else} part, it doesn't. The else part is executed once, an item is added to array. So far so good. However, now the {section show=$my_array|contains($counter)} always evaluates to true, and the new element in the array is updated with the value of $counter (although the else part of the loop is isn't executed??).

I am at a loss here.

Paul Forsyth

Thursday 10 June 2004 5:07:05 am

Are you sure your array is populated correctly? What does {$my_array|attribute(show)} produce?

paul

--
http://www.visionwt.com

Rinze Cats

Thursday 10 June 2004 5:09:26 am

Mm, solved it myself, but am wondering if this is by design or if it is a bug.

Instead of using $counter, I assign it to a variable

{let my_count = $counter}

and use my_count in the function.

When using $counter, it adds $counter itself to array, and then startupdating it in the array. So {$counter} prints the number, with let you can assign it, but with append you append the variable instead of it's value.

Paul Forsyth

Thursday 10 June 2004 5:12:41 am

That is weird. Yes, file it as a bug. $counter should be the value of the current element of the loop. If its an integer it should act like one...

paul

--
http://www.visionwt.com

Rinze Cats

Thursday 10 June 2004 5:15:24 am

thanx for all the help paul, will report this is a bug

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.