2

I've been able to prevent caching of my footer and some other sections, but am having trouble doing so with one specific section of my home page. On my home page, the section in question is called using:

{{block type="core/template" name="homepage-highlights" alias="homepage-highlights" template="jl/homepage/homepage-highlights.phtml"}}

In my local.xml file, I already have another section defined:

<cms_index_index>

    <reference name="root">
         <block type="core/template" name="homepage-billboardSection" as="homepage-billboardSection" template="jl/homepage/homepage-billboardSection.phtml" />
         <action method="setBlockId"><block_id>homepage-billboardSection</block_id></action>
    </reference>

    <reference name="homepage-billboardSection">
        <block type="core/template" name="billboard" as="billboard" template="jl/modules/billboard/billboard.phtml" >
        <action method="setBlockId"><block_id>billboard</block_id></action>
        </block>
    </reference>

</cms_index_index>

The phtml file that contains what I'm trying to exclude from getting cached is in

jl/homepage/homepage-highlights.phtml

I was able to disable caching of the footer by adding:

<reference name="footer">
    <block type="core/template" name="js_files" template="page/html/js.phtml"/>
    <action method="unsetData">
       <key>cache_lifetime</key>
    </action>
</reference>

to local.xml but I'm not seeing how to do the same for this section of my home page. Any thoughts would be welcomed.

3 Answers 3

3
{{block type="core/template" name="homepage-highlights" alias="homepage-highlights" template="catalog/navigation/left.phtml" cache_lifetime="false"}}

just add new parameter cache_lifetime="false" at end of default parameters

1
  • Great solution!
    – Bob
    Commented Jan 3, 2017 at 23:57
2

Your sample code is a bit confusing. For example, I don't see how your layout update handle cms_index_index relates to the block in question. Your action node on the root block won't serve a purpose, because root is an instance of Mage_Page_Block_Html and doesn't make use of setBlockId.

Nevertheless, your block in question is generated using a CMS block directive {{block}}, so it will be cached along with the CMS content. To undertand why this is so, have a look at Mage_Core_Block_Abstract::toHtml

final public function toHtml()
{
    Mage::dispatchEvent('core_block_abstract_to_html_before', array('block' => $this));
    if (Mage::getStoreConfig('advanced/modules_disable_output/' . $this->getModuleName())) {
        return '';
    }
    $html = $this->_loadCache();
    if ($html === false) {
        $translate = Mage::getSingleton('core/translate');
        /** @var $translate Mage_Core_Model_Translate */
        if ($this->hasData('translate_inline')) {
            $translate->setTranslateInline($this->getData('translate_inline'));
        }

        $this->_beforeToHtml();
        $html = $this->_toHtml();
        $this->_saveCache($html);

        if ($this->hasData('translate_inline')) {
            $translate->setTranslateInline(true);
        }
    }
    $html = $this->_afterToHtml($html);
    ...

You can see that a cached version of the block is requested from cache. In this case we're talking about Mage_Cms_Block_Page. If no cache was found ($html === false) then we'll generate it as new. It's in that conditional block that your block directive will get converted into a block and rendered along with the CMS page content.

And as you'll also see, post-render that data is cached. So subsequent retrieval of it will yield the same copy.

Alternative Options

A: Use the Design tab of your CMS Page edit screen to add custom layout XML, and add to it an unsetData action on the cms_page block to remove cache lifetime like you did with the footer.

Example:

<reference name="cms_page">
    <action method="unsetData">
        <key>cache_lifetime</key>
    </action>
</reference>

CMS page layout XML updates

The drawback here is that you're no longer caching the CMS page, but at least it will be isolated to only this page.

**Haven't tested the above, but I suspect it would work!

B: If possible, add the block to another part of the layout, maybe to content just before cms_page -- not sure how that impacts your layout. In a worst case scenario, you could write some quick JavaScript re-positioning on dom:loaded to move that block into place. If this is for a very specific area like the homepage and perhaps even something to be used for a short period of time, there's no shame in a "quick 'n' dirty" solution, IMO.

3
  • Thanks for the reply. I too was confused by the code (I didn't write it). I suppose I could settle for not caching the entire home page and I considered adding some custom layout XML to the CMS page. I'm just not sure what to add there. I tried <action method="unsetData"> <key>cache_lifetime</key> </action> but that didn't see to work. Commented Jun 9, 2015 at 20:44
  • I've updated my answer @dsb_magento. See if that helps. Commented Jun 9, 2015 at 20:54
  • That doesn't seem to have worked but I still appreciate the help. My goal here is to have the display of the value of a session variable get updated, but with this code in place I still have to flush Magento's cache manually for the change to show up. Commented Jun 10, 2015 at 12:25
0

You can do it using Block file.

$cache = Mage::app()->getCacheInstance();
 // Tell Magento to 'ban' the use of block
 $cache->banUse('block_name');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.