Cannot retrieve block from layout when full page cache is enabled
-
16-10-2019 - |
Question
The problem is I cannot retrieve any block from layout or child block from my cached block when full page cache is enabled. I have next files: 1) cache.xml
<config>
<placeholders>
<socialfeed_posts>
<block>onepica_socialfeed/country_story</block>
<name>country_story</name>
<placeholder>SOCIAL_COUNTRY_STORY</placeholder>
<container>OnePica_SocialFeed_Model_Cache_Container_Country_Story</container>
<cache_life>0</cache_life>
</socialfeed_posts>
</placeholders>
</config>
2) OnePica_SocialFeed_Model_Cache_Container_Country_Story.php
class OnePica_SocialFeed_Model_Cache_Container_Country_Story extendsEnterprise_PageCache_Model_Container_Abstract
{
protected function _renderBlock()
{
$blockClass = $this->_placeholder->getAttribute('block');
$template = $this->_placeholder->getAttribute('template');
/* @var $block OnePica_SocialFeed_Block_Country_Story */
/*$block = new $blockClass;
$block->setTemplate($template);
$layout = Mage::app()->getLayout();
$block->setLayout($layout);
return $block->toHtml();
}
}
3) OnePica_SocialFeed_Block_Country_Story.php
class OnePica_SocialFeed_Block_Country_Story extends Mage_Core_Block_Template
{
...
public function getPager()
{
if (null === $this->_pager) {
$this->_pager = $this->getLayout()->getBlock('social_feed_pager');
}
return $this;
}
...
}
4) layout.xml
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
<block type="onepica_socialfeed/html_pager" name="social_feed_pager" template="onepica/socialfeed/page/html/pager.phtml" />
<!-- Tried as a simple block of layout-->
</reference>
<reference name="content">
<block type="onepica_socialfeed/country_story" name="country_story" as="country_story" template="onepica/socialfeed/country/story.phtml" >
<block type="onepica_socialfeed/html_pager" name="social_feed_pager" template="onepica/socialfeed/page/html/pager.phtml" />
<!-- Tried as a CHILD block of my cached block-->
</block>
</reference>
When page load at first all be right, but when I tried reload page second time, page is crached, due to $this->getLayout()->getBlock('social_feed_pager') row. I don't want to create 'social_feed_pager' block in my Block using $this->getLayout()->createBlock('social_feed_pager'). I need retrieve block from layout.
Any help would be much appreciated. Thanks!
Solution
Seems like your problem is that cache_lifetime of your block is set to '0'. That means your block is not cache at all and its code runs during each reload but all other blocks are in cache so you cannot get them like getLayout()->getBlock('any_block')
.
Try to set
<cache_lifetime>86400</cache_lifetime>
After clear all cache.