Forums / Developer / Cache-block problem
Salvatore Guarino
Thursday 03 May 2007 6:25:40 am
Hi, I have a strange problem with caching. I enabled on my site.ini.append.php the following settings:
[ContentSettings] ViewCaching=enabled [TemplateSettings] TemplateCache=enabled TemplateCompile=enabled
In my pagelayout.tpl I have a cache-block with a variable that changes a div id when the section changes:
{cache-block keys=$uri_string} {def $section=fetch( 'section', 'object', hash( 'section_id', $node.object.section_id ) )} <div id="topcontent-{$section.name|wash}"> ... </div> {/cache-block}
If I clear the cache, the site works and if I navigate to differents sections the div id changes. But, sometimes, it doesn't work at all because div id doesn't change untill I clear the cache again. Exponential version is 3.7.2.
Thank you!
Nabil Alimi
Thursday 03 May 2007 7:05:43 am
Hi Salvatore,
You're facing the very unfamous $node availibility issue. Until you fetch the <b>$node</b> by yourself, with something like :
{def $node=fetch( content , node , hash( node_id , $module_result.node_id )
you should <b>never</b> use the $node variable in pagelayout. It is an issue that has been <i>solved</i> in recent Exponential releases by totally removing the $node variable thus making it no longer possible to do what you're trying.In previous versions, the $node was available on first-time access of a node that's why your piece of code works on your 3.7.2 Exponential version.
So here, as I've suggested earlier, you'll need to fetch the $node before using it.
:)
Best regards.
My blog : http://www.starnab.com/ezpublish / http://www.starnab.com/ / http://www.assiki-consulting.com eZ Publish Freelance developper. Feel free to contact me +33 674 367 057 nabil at assiki d0t fr
Thursday 03 May 2007 7:27:37 am
Hi Nabil, thank you very very much for your respone, but I don't understand what do you mean with <i>"you'll need to fetch the $node before using it"</i>. Could you give me an example?Bye, Salvatore
Thursday 03 May 2007 9:26:30 am
Salvatore,
I meant that you need to fetch the node with something similar to the piece of code I pasted up there. Try this :
{cache-block keys=$uri_string} {def $node=fetch( 'content' , 'node' , hash( node_id , $module_result.node_id ) ) $section=fetch( 'section', 'object', hash( 'section_id', $node.object.section_id ) )} <div id="topcontent-{$section.name|wash}"> ... </div> {/cache-block}
In case you need to access the $node somewhere else in your template, you should put the node fetch outside of the cache-block.
Atle Pedersen
Friday 04 May 2007 1:06:28 am
The point/problem is that you're not always showing a node. Sometimes you show views from other modules, for example the login screen. In these cases having a $node variable is meaningless.
Therefor you should also check for the existence of $module_result.node_id before using it.
Read more on this friendly page: http://ez.no/doc/ez_publish/technical_manual/3_8/templates/the_pagelayout/variables_in_pagelayout