1

in some case few block need to be dynamic if you use full page at that time we need to turn off cache only for that Individual block for that we call Hole Punch

to do that follow below steps
1. create cache.xml file in AW/Zblocks/etc/cache.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <placeholders>
        <deptlhpromo>
            <block>core/template</block>
            <template>zblocks/deptlhpromo.phtml</template>
            <name>deptlhpromo</name><!-- THIS MUST MATCH THE ALIAS IN THE LAYOUT XML EXACTLY -->
            <placeholder>DEPTLHPROMO</placeholder>
            <container>AW_Zblocks_Model_Container_Deptlhpromo</container>
            <cache_lifetime>86400</cache_lifetime>
        </deptlhpromo>
    </placeholders>
</config>


2. Create Container model that is AW/Zblocks/Model/Container/Deptlhpromo.php

class AW_Zblocks_Model_Container_Deptlhpromo extends Enterprise_PageCache_Model_Container_Abstract
{
    protected function _getCacheId()
    {
    return 'DEPTLHPROMO_' . md5($_COOKIE["frontend"]);
    }

    public function applyInApp(&$content)
    {
        $block = $this->_placeholder->getAttribute('block');
        $template = 'zblocks/deptlhpromo.phtml';

        $block = new $block;
        $block->setTemplate($template);
        $block->setLayout(Mage::app()->getLayout());    

        $blockContent = $block->toHtml();

        $this->_applyToContent($content, $blockContent);

        return true;
    }

    protected function _saveCache($data, $id, $tags = array(), $lifetime = null)
    {
        return false;
    }

}
  1. Create phtml which will called once page is cache else it will call default xml (in our ex it will be from catalog.xml)/template/zblock/deptlhpromo.phtml

    echo Mage::app()->getLayout()->createBlock('zblocks/block')->setPosition('DEPT_LH_promo')->toHtml();


4. catalog.xml where default loaded if page is not cache that is at first time

<catalog_category_default translate="label">
        .
        .
        .
        .
        .
        <reference name="content">
            <block type="catalog/category_view" name="category.products" template="catalog/category/view-category.phtml">  
                <block type="core/template" name="deptpromo">
                    <action method="setTemplate">
                        <template>zblocks/deptpromo.phtml</template>
                    </action>
                </block>     
                <block type="catalog/product_list" name="product_list" template="catalog/product/list.phtml">
                .
                .
                .
                .
                .
         </catalog_category_default>

Imp Note :- in catalog.xml block name should be same as in cache.xml place order that is

    should be same as 


<config>
            <placeholders>
                <deptlhpromo>
                    <block>core/template</block>
                    <template>zblocks/deptlhpromo.phtml</template>
                    <name>deptlhpromo</name><!-- THIS MUST MATCH THE ALIAS IN THE LAYOUT XML EXACTLY

then only it works

  • Please add question first, to which this post serves as an answer, and then add this post as the answer. This is how SE works. – Prateek Oct 14 '15 at 12:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.