Variables scope

Author Message

Shurbann Martes

Thursday 15 January 2004 8:30:37 am

Hello,

I'm having problems using variables in sections:

[code]

{let var=1
$array=(1,2,3,1,4) }
{section name=Section1 loop=$array}
{$var}
{section show=not($array|contains($var))}
......

{/section}
{/section}
{/let}

[/code]

This is maybe not a great code, it's just an example).
My problem is that $var has no value in the section Section.
What's the problem here? Does it have something to do with the scope? What can I do to solve this problem?

Regards,
-Shurbann

Paul Borgermans

Thursday 15 January 2004 11:13:15 am

Hi Shurbann

---edited---
Changed the
{section show=$array|contains($Section1:item)|not} into
{section show=eq($var,$Section1:item)|not}
to guess what you wanted
--------------

Your example is full of errors, this works (copy and paste, the comments explain your errors):

{let var=1
array=array(1,2,3,1,4) }
{* use array=... , not $array= in let constructs*}
{* arrays need the array creator operator *}
{section name=Section1 loop=$array}
{$var}
{*this will display 1 for every time the loop gets executed , ie 5 times a 1*}
{section show=$array|contains($var)|not}
{* use the notoperator always with {$something|not} *}
{* even more : don't surround anything with parenthesis for evaluation by not, just read from left to right *}
{* what do you want to do here? it is always false*}
{*
you probably wanted to do
{section show=eq($var,$Section1:item)|not}
....
{/section}
*}
{/section}
{/section}
{/let}

More in the upcoming book on eZ publish ;-)

hth

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Paul Borgermans

Thursday 15 January 2004 11:29:16 am

Oh yeah,

The error has nothing to do with namespaces here: variables from "outer sections" including the "root" of your template should always be visible.

To put another example illustrating namespaces:

{let myvar=2}
{section name=innersection}
{let myvar=1}
<p>root myvar: {$myvar}</p>
<p>section myvar: {$innersection:myvar}</p>
{/let}
{/section}
{/let}

Nice, right?

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

Willie Seabrook

Thursday 15 January 2004 3:19:09 pm

Did someone say 'book on ezpublish'? Ooooh! ;-) Paul who's writing it, is it open & free and whens it coming out?
If its open and free (essentially just more documentation) I'd be only too happy to contribute a few pages on a designated subject. At least it would put all those hours I've spent stepping through the ez kernel code in the Zend debugger to good use!

Regards,
Willie

Paul Borgermans

Friday 16 January 2004 5:29:58 am

No

It won't be free. It will be released probably during the first quarter of 2004 (Packt publishing). I can't say more because I don't know more except that I'm not the only one contributing to this book.

cya

-paul

eZ Publish, eZ Find, Solr expert consulting and training
http://twitter.com/paulborgermans

K259

Saturday 17 January 2004 12:12:45 pm

Hi!

Interesting with this book. I'm also working on a book about eZ publish, think it will be finished around summer 2004(I hope).

What are your content and main topics?

K259

Saturday 17 January 2004 12:19:32 pm

:) Found the description
http://www.packtpub.com/site/books.html

Are the contributors in this book explaining the documentation from www.ez.no and taking examples from the ez-doc. (or showing new examples with complete code?), and which userlevel of understanding will this book apply to?

Shurbann Martes

Sunday 18 January 2004 2:54:40 am

Thanks for the replies.

I invented that code on the fly when I was posting this, so I knew it had alot of error, but the idea was to have one of you telling me how to get around with variables in different section.

Paul: I'll take a look at your corrections

Thnx,
-Shurbann

Shurbann Martes

Sunday 18 January 2004 3:27:40 am

It's better to show the code I'm working on. I want to show on my frontpage the +/- 8 articles/news where people commented lately, without showing the article or news more then once.

[code]

{let comment_parent_array=array(0) // make an array, where I will put the article/news id's
comment_list=fetch(content,list,hash(parent_node_id,2,limit,12,depth,5,sort_by,array(published,false()),class_filter_type,include,class_filter_array,array('comment')))} //fetch all the latest comments
{section name=Comment loop=$comment_list} //loop trough the comments
{section show=$comment_parent_array|contains($Comment:item.parent_node_id)|not} // if the article/new has already been shown don't go trough this secton
{let $comment_parent_array|append($Comment:item.parent_node_id)} //put the id of the article/news in the array
{node_view_gui view=line content_node=$Comment:item} //show the comment
{/section}
{/section}
{/let}

[/code]

It keeps showing the articles more then once. So one way or the other the idea of checking the array or putting the parent_node_id in the array isn't working.

I have run {$comment_parent_array|attribute(show,1)} in the code above and still the array has only the value 0 (the way I defined it at the beginning) in all the sections.

I'm out of ideas. Can you see the problem?
-Shurbann

STEVO +

Monday 19 January 2004 7:57:13 am

i'm after doing something like this myself. the problem is you can read but not alter variables that were defined in outer sections, which is understandable.

i might have a look at making some kind of extension to do this.

Shurbann Martes

Wednesday 21 January 2004 11:37:19 pm

Stevo C,

Yes,that's what is missing with the variables in sections.
Let me know how far you are with the extension and if you need some help.

-Shurbann

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