Can't access my vars!

Author Message

Thomas Brandl

Monday 15 September 2003 6:00:27 am

Hi everybody,

I have a default statement in my pagelayout defining a couple of variables. I can access them inside pagelayout.tpl without any problem.

{default color_1=$background.0.data_map.color_1.data_text}
($background was fetched before)

I cannot access them from within any other template with {$#color_1}, error message is: "Unknown template variable 'color_1' in namespace '' " ({$color_1} isn't working neither, oc)

I thought, I had this working once....

So, how can I define and access global variables?

Thanks!!
t

Paul Borgermans

Monday 15 September 2003 6:23:29 am

Good question, would like to know too.

http://ez.no/developer/ez_publish_3/documentation/development/libraries/ez_template/functions/include

You can always pass them when including them.

-paul

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

Thomas Brandl

Monday 15 September 2003 7:05:28 am

Thanks Paul, but still no luck!

With this approach, the vars aren't even visible to my pagelayout anymore!
I did this:
- create a template with a {let fetch=..} and a default block.
- Include it in pagelayout
- Try to access the variables ..

The thing is, I am closing the let-and default block inside the include. Aren't they freed when you close the block? So after the include, they do not exist anymore...

I just found out, that I can see the variables inside templates, that are included by pagelayout, but I can't see them inside {$module_result.content}. But that's where I need them!

Sigh...
t

Paul Forsyth

Monday 15 September 2003 7:12:17 am

This is puzzling. Could you perhaps give a distilled example of your code with the template logic you use? Its hard to see exactly where your problem starts...

thanks

paul

Thomas Brandl

Monday 15 September 2003 7:47:50 am

Sure, try to keep it as simple as possible:

Approach No 1:
pagelayout.tpl:
{let bg=fetch(content,list,...)}
{default color_1=bg.0.data_map.color_1.data_text}
<html>
<body>
this works: {$color_1} is my color.
{include uri='mynav.tpl'} {* below *}
{$module.result}
</body>
</html>
{/default}
{/let}

mynav.tpl:
this works too {$color_1} or {$#color_1}

---------------------------
Approach No 2:
pagelayout.tpl
<html>
<body>
{include uri='someincludes.tpl'}
{$color_1} and {$#color_1} are not there!
{$module.result}
</body>
</html>

someincludes.tpl:
{let bg=fetch(content,list,...)}
{default color_1=bg.0.data_map.color_1.data_text}
{/default}
{/let}

But whatever happens inside {$module.result}, the color_1 var cannot be accessed. I need them later in override-templates.

I'm not sure whether this behaviour is by design, but there must be a way to define global (really global!) variables, no?

Thanks guys!
t

Paul Forsyth

Monday 15 September 2003 8:37:33 am

Ah, this is a scope issue. Normally any variables you set within an include will be released when you come back from that include. What you are doing may not be possible. Also your included template has a {let}{/let}. When the template code reads the final {/let} the variables defined by your initial {let} will be released.

This thread discusses a little about your problem:

http://ez.no/developer/ez_publish_3/forum/developer/using_letset_within_namespaces

If i were you i'd get rid of the default lines and include the variable assignment in the top level {let}. Also, try not specifying the final {/let}.

For example:

pagelayout.tpl
{let name=MyScope
-----color_1=0}
{include.....}
{/let}

included_file:

{let bg=fetch(content,list,...)}
{set color_1=$MyScope:bg.0.data_map.color_1.data_text}
{/let}

This should let you use color_1 outside of your lets...

You may have more luck trying this approach but i honestly don't know if it will work. Im not sure if ez templates allow you to pass variables back from one template to another...

paul

Thomas Brandl

Monday 15 September 2003 9:18:21 am

I also tried the let/set combination, without success.
There's actually no special demand for me to use includes.tpl, all I need is all other templates to be able to access the vars defined in pagelayout.tpl.

{set-block} made me hope for a second, bc you can specify a scope for it. the root scope however doesn't include {$module_result.content} neither.

Too bad it doesn't seem to be possible by now!
sigh again...

Have a nice evening!
t

Paul Forsyth

Tuesday 16 September 2003 12:22:15 am

Yip, this aspect of template programming is weak. Hopefully it will improve soon, along the lines spoke about in the link i gave.

Paul

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