Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Magento 1.4/1.5 Disable Block HTML Output Cache
 
alexandru.topliceanu
Jr. Member
 
Total Posts:  8
Joined:  2011-01-11
 

Hi everyone,

I am building a custom module that exposes some js variable of the page through a block template loaded in each page in the footer.
These variables change depending on the “type” of the page, but magento caches the output of the block, so the variables don’t change.
I need to programatically ensure that the html output of the block is not cached by magento. I can’t force the buyers of my module to disable all block cache on their store smile
I have found a solution to enable block caching( ?!?! caching is enabled by default ) and adapted it to serve my purposes, but it doesn’t seem to work.

class Namespace_Block_Blablabla
     
extends Mage_Core_Block_Template {

        
// prevent caching of block's output
        
public function getCacheKey(){
            
return $this->_getSqPageType() ; // this can be one of the following "product" | "checkout" | "confirmation" | "normal page"
        
}
        
public function getCacheLifetime(){
            
return // smalles cache time possible, i want to disable it completely but i cant find a solution
        
}
        
public function getCacheTags(){
            
return array( Mage_Catalog_Model_Product::CACHE_TAG ) ; // i don't know what this does
        
}
        
      
public function __construct() {
       
                  parent
::__construct() ; 

          
//should prevent caching of block's output, some solutions i found online say this is not needed, it doesn't work either way
          
$this->addData( array(
                  
'cache_lifetime'=> $this->getCacheLifetime() ,
                
'cache_tags'    => $this->getCacheTags() ,
                
'cache_key'        => $this->getCacheKey()
          )) ;
         
}
It is really important that i fix this as we need to release this module as soon as posible

Thanks for your feedback,
Alex

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagentoECG
Magento Team
 
Avatar
Total Posts:  189
Joined:  2011-02-25
Worldwide
 

Hi.

To disable cache in your block you just need to override the method ‘getCacheLifetime’ as the following:

public function getCacheLifetime()
    
{
        
return null;
    
}

You don’t need to override all those methods which you have submit here. Overriding of getCacheLifetime() is enough.

<AM>

 
Magento Community Magento Community
Magento Community
Magento Community
 
alexandru.topliceanu
Jr. Member
 
Total Posts:  8
Joined:  2011-01-11
 

Hi,

Thanks for the fast reply, but it doesn’t seem to work.
Maybe it’s the way magento caches js files.
My block’s template looks this :

<script type="text/javascript">
         var 
jsVariable1 '<?php echo phpVariable1; ?>' ;
                 var 
jsVariable2 '<?php echo phpVariable2; ?>' ;
                 
//.....so on
    
</script>
The contents of this script do not change if i set admin->system->index management->Blocks HTML Output to “enable”
I should mention that i tested my module with “Blocks HTML output” disabled and it works perfectly.
I am testing this in magento1.4.2.0 and magento1.5.0.1

Thanks,
Alex

 
Magento Community Magento Community
Magento Community
Magento Community
 
MagentoECG
Magento Team
 
Avatar
Total Posts:  189
Joined:  2011-02-25
Worldwide
 

The problem is that you insert your block in footer block.
Natively Magento caches footer block on infinity time with all its children.

So, you can add your block as a child to ‘before_body_end’ block, not to ‘footer’.

<AM>

 
Magento Community Magento Community
Magento Community
Magento Community
 
alexandru.topliceanu
Jr. Member
 
Total Posts:  8
Joined:  2011-01-11
 

Indeed this works,

Thank you!

 
Magento Community Magento Community
Magento Community
Magento Community
 
muk_t
Sr. Member
 
Total Posts:  226
Joined:  2012-07-24
 

Hi,

I am facing problem with \” Blocks HTML output \” cache enabled in Magento Enterprise edition 1.12.0.0.

My navigation user interface is not working well.If I disable the above cache every thing works well please suggest some solution.

I am using topmenu.phtml for navigation.

I also posted these links but no reply was there
1)
link1

2)link 2

My page.xml file has code like this

<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
          <
label>Navigation Bar</label>
           <
block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml"/>
 </
block>

Please suggest some solution
Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top