Magento – CMS Block Cache issue with included dynamic template

blockscachece-1.9.2.0cmsstatic-block

I've seen lots of issues with CMS block caching in Magento 1.9.2.0 and have an issue where I've created a static block that loads the current category ID and then displays variables that have a matching name as the category (as well as a few other flags)

this means that the same static block can be displayed on multiple pages, without having to create multiple instances

The problem:

with block html cache enabled, the block displays correctly on the first page that is viewed. If however you navigate from the first category page to another that calls the same cms block (which should display new variables), the block you just viewed is displayed on that second category page

I've tried using the recommended Rkt cache fix, however I suspect that because I'm re-using the block dynamically that it will always have the same block id and therefore the fix will not work.

The CMS Static Block contains {{block type="core/template" template="template/category-block.phtml"}} which calls the custom phtml file. It uses category ID to find the variables.

Best Answer

I suspect that because I'm re-using the block dynamically that it will always have the same block id and therefore the fix will not work

That's right. And by adding dynamic content to a static block you are using it not as it is intended. You should use layout XML to add dynamic blocks with more flexibility.

If you insist on adding the content by selecting a static block in the category, you will need one static block for each category which then includes the dynamic template with different parameters. Which brings us to the next problem:

the CMS Static Block contains {{block type="core/template" template="template/category-block.phtml"}} which calls the custom phtml file.

This one is cached as well.

The inserted block is not a CMS block (cms/block) but a regular template block (core/template) so it does not even have a block id. But the problem stays: the cache key is the same for both instances.

This is how the cache key is generated for core/template blocks:

public function getCacheKeyInfo()
{
    return array(
        'BLOCK_TPL',
        Mage::app()->getStore()->getCode(),
        $this->getTemplateFile(),
        'template' => $this->getTemplate()
    );
}

See Mage_Core_Block_Template

The actual cache key is a sha1 hash of all array values joined with "|". But you also can manually specify any unique string as cache key, for example:

{{block type="core/template"
        template="template/category-block.phtml"
        cache_key="CATEGORY_BLOCK_DEFAULT_STORE_CATEGORY_42"}}
Related Topic