Forums / Developer / global variable

global variable

Author Message

Rodrigo Costa

Friday 01 August 2008 11:28:36 am

Hi!

In my pagelayout, I am defining a variable, out of cache-block.

{def $var}
{set scope='global' $var='Test'}

Then, in another template, I try to show this var:

{$#var}

The problem is that this doesn't work and do not display anything.

How can I pass variable between templates??

Tx!!

Rodrigo Costa

Thursday 14 August 2008 5:28:55 am

anyone could help me? please!

André R.

Thursday 14 August 2008 6:41:56 am

cache blocks cache to the template output result (mostly xhtml), so you can not use variables defined inside a cache block, outside it.

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

Russell Michell

Thursday 14 August 2008 3:07:22 pm

Hi Rodrigo,

You could try a slightly different approach. Of course it really depends on what it is you're trying to achieve - I mean, if in your extension (for example) you have multiple templates that require access to this same variable, simply see if you can combine the templates and use different sections of code within that template depending on a user action or settings change or whatever.

You could also include your template where your $var is defined, inside another template. Just look at pagelayout.tpl for how this is done.

The other, slightly more involved solution is to make use of eZ's "Custom Tags". (http://ez.no/doc/ez_publish/technical_manual/4_0/reference/datatypes/xml_block/custom_tags) The general idea with these is that you build a small template file and store it in a specific directory. The file is like an HTML fragment, not complete page. You then reference that template via the online editor's "insert object" dialogue box.

What this means is, you can insert dynamic content, directly into the online editor and in <b>multiple pages</b>. So when the content of your template changes (say due to user posting some data to a custom class of your own) the content also changes in every page you have included this template.

If this last solution sounds ideal, do what I did and take half a day or so to complete a tutorial and read the docs carefully. It really is well-worth it!

Good luck :-)
Russ

Russell Michell, Wellington, New Zealand.
We're building! http://www.theruss.com/blog/
I'm on Twitter: http://twitter.com/therussdotcom

Believe nothing, consider everything.