4

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.

  • Are you using static block via CMS? I believe that if you create a block via local.xml changing the cache parameters, the problem will be solved. Are you using any full page caching? – Denis Spalenza Sep 11 '15 at 12:59
  • the CMS Static Block contains {{block type="core/template" template="template/category-block.phtml"}} which calls the custom phtml file. As far as I'm aware, we are using Magento's cache and some leveraged browser caching only – Duncan Wardlaw Sep 11 '15 at 14:12
  • Are you using this static block only on category pages? If not, on which pages this static block is used? – Denis Spalenza Sep 11 '15 at 14:29
  • only category pages - as it uses category ID to find the variables, I've set it to display nothing if it's added to a CMS page – Duncan Wardlaw Sep 11 '15 at 14:48
5

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"}}
5

In a phtml file, if you need to instantiate a block without cache you can use this:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_id')->setCacheKey(uniqid())->toHtml();

In the block you can instantiate the page/content that you need like this:

{{block type="core/template" template="page/some_page.phtml"}}

or just write the content directly in there some_page.phtml is in app/design/frontend/base/default/template/page/

Hope it does it for you!

  • Thanks. I can now create a page template and use it multiple times now in any cms pages. In cms_image.phtml: <?php $sku = $this->getProduct_sku(); $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku); $imageSize = $this->getImage_size(); ?> .... block: {{block type="core/template" product_sku="1234" image_size="196" template="page/cms_image.phtml"}} – xinqiu Jun 2 '17 at 5:23
4

Remove the block (disable is best for testing purposes) the static block and try use layout to create this block.

It's look like this:

local.xml file in your theme: Create blocks in content block

<catalog_category_default>
// [...] Other layouts ...
    <reference name="content">
        <block type="core/template" before="category.products" template="template/category-block.phtml">
            <action method="setCacheLifetime"><s>0</s></action>
        </block>
    </reference>
// [...] Other layouts ...
</catalog_category_default>

<catalog_category_layered>
// [...] Other layouts ...
    <reference name="content">
        <block type="core/template" before="category.products" template="template/category-block.phtml">
            <action method="setCacheLifetime"><s>0</s></action>
        </block>
    </reference>
// [...] Other layouts ...
</catalog_category_layered>

*OBS: In the example above I'm assuming yourcustom block will be the beginning of the category page (before category.products block).

  • Thanks Denis - as the block won't appear in all categories, I'd ideally like a way to continue to call the block by setting a static block through the CMS – Duncan Wardlaw Sep 11 '15 at 16:54
  • Hum, now I got it. With some changes in phtml we can get this info of a category object. Could you provide your phtml? There is other way in layout to do it in specific categories but it's gonna be static (not a good solution to admin) – Denis Spalenza Sep 11 '15 at 17:15
2

I'm adding in my answer only because this was exactly my problem and the solutions provided didn't exactly fix what i needed, so in case someone comes along and needs one more alternative.

For the site i'm working on the site owner wanted to have two different templates for category sections, and be able to pick that template themselves, without us being able to say when or where they were going to use it.

So I added in the template file into the catgegory with xml in the Custom Layout Update section (Catalog-> Manage Categories-> Custom Designs-> Custom Layout Update). This give the store owner the ablity to change the template at will with just a tiny admin change.

<reference name="content">
   <block type="catalog/navigation" name="category_landing_page" template="catalog/category/category-landing.phtml">
      <action method="setCacheLifetime"><s>0</s></action>
   </block>
</reference>

Your Answer

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

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