Forums / Developer / Recall a variable defined inside an included template from frontpage.tpl

Recall a variable defined inside an included template from frontpage.tpl

Author Message

Lo' F.

Tuesday 16 November 2010 3:27:42 am

Am I asking too many questions in this forum?! Please, don't blame me for this!!

...Hi! It's me again!

Well, I have been searching for a way to set a variable inside a template included in the frontpage.tpl and then call that variabe from the frontpage.tpl itself, but I cannot make it work...

- inside included_template.tpl declared $item_title variable...

{def $items=fetch('content', 'tree', hash('parent_node_id', 2,...,'limit', 1, 'attribute_filter', array(array('item_attribute/selected_item', '=', 1 ))}
{foreach $sitems as $item} 
    {def $item_title = $item.data_map.titolo.content}
{/foreach}

by simply recalling the variable from frontpage.tpl it does nothing..

- frontpage.tpl

<div> 
   {include uri='design:content/included_template.tpl'}
 </div>
 <p>{$item_title}</p>

...

Second attempt:

- set the global scope to a new variable where I pass the value of the "local" one inside the included template..

...
{def $sel_item_title=""}
{set $sel_item_title=$item_title scope='global'}

- and recalled that global variable from frontpage.tpl

<p>{$#sel_item_title}</p>

...yet nothing...

Any hint would be really appreciated!

loredanaebook.it

Lo' F.

Tuesday 16 November 2010 3:55:26 am

I must say.. this forum is lucky to me... sometimes only by posting, it gives me the idea !

Actually it's not the answer to my question, but I made it work somehow:

I just relocate the fetch and the variable declaration inside frontpage.tpl above the line where I include the external template. I use that variable in here as well as in frontpage.tpl and doing in this way both the templates takes the variable.

It works and that's quite ok for now but it would be great if I could make tail of why leaving things like before didn't give any result..!?

loredanaebook.it

Gaetano Giunta

Tuesday 16 November 2010 4:12:40 am

It did not work because of scoping.

Scoping in eZP templates has been defined "borderline blackmagic" in the past, and my advice about it is doing exactly what you found out on your own: define a var in the pagelayout, then it will be available later on in the included templates.

You can even set its value in the included template, and the new value will be available after the inclusion in the pagelayout itself.

One caveat really: try to fetch as little as possible in the pagelayout, as those fetches are executed on every page view.

Generally to reduce the number of pagelayout fetches cache-blcoks are used. But when doing so, you cannot used included templates anymore, unless they're included within the cache block.

Principal Consultant International Business
Member of the Community Project Board