2

As magento1.9.2 is added cachetags function in to construct functon i must have to give seperate cachetags for each block element because its get conflict with varnish to read key. i have solved this issue in.phtml file to set key like below

 $this->getLayout()->createBlock('cms/block','',
         array(
         'cache_lifetime' => false,
         'cache_tags' => array(Mage_Cms_Model_Block::CACHE_TAG."_twocmspages")
                          ))->setBlockId("twocmspages")->toHtml(); 

but if need to assign same cache_tags using xml with setMethod how can i do this.

<block type="cms/block" name="block_id">
... set cachetag here
</block>

Is any one has idea how to add cache_tag array arrgument from XML ?

your help will be appreciated here.

2 Answers 2

5

Arguments in <action> can be defined as nested elements and will be converted to array:

<block type="cms/block" name="block_id">
    <action method="setTags">
        <tags>
            <tag1>cms_block_twocmspages</tag1>
            <tag2>( just in case you want to add more tags )</tag2>
        </tags>
    </action>
</block>

It should be noted that <tag1> and <tag2> will be the array keys, so you cannot use the same tag name and you cannot pass arrays with numeric indexes because XML tags cannot be numbers.

A more flexible solution is using JSON. Just specify which argument should be parsed as JSON with the json attribute of <action>:

<block type="cms/block" name="block_id">
    <action method="setTags" json="tags">
        <tags>["cms_block_twocmspages"]</tags>
    </action>
</block>
4
  • ha. it works with json=...? I never knew that. Can you tell me where this is parsed because I'm really curious. Commented Jul 22, 2015 at 14:49
  • 2
    Never mind. I found it: github.com/OpenMage/magento-mirror/blob/magento-1.9/app/code/…. Good to know. Commented Jul 22, 2015 at 14:51
  • thanks for your answer, not working with json like this <action method="setCacheTags" json="CacheTags"> <cachetags>["cms_block_twocmspages"]</cachetags> </action> will you please make me correct ? it's giving me fatal error :( Commented Jul 22, 2015 at 14:56
  • 1
    The elements are case sensitive, so if you use <cachetags>, it has to be json="cachetags" Commented Jul 22, 2015 at 14:58
2

Not sure if you can do it directly from the layout but you can do a trick.
Everything parameter you pass to an <action> tag can come from a helper.

Something like this:

<block type="something/here" name="some_name">
    <action method="setSomething">
        <something helper="module/getSomething" />
    </action>
</block>

This means that the method setSomething from the block something/here will be called and will receive as parameter the result of Mage::helper('module')->getSomething().

So you can try the same for your code:

<block type="cms/block" name="block_id">
    <action method="setCacheTags">
         <tags helper="modulename/getBlockTags" />
    </action>
</block>

now just implement the method getBlockTags() in the helper Namespace_Modulename_Helper_Data that returns the array with your tags.

If you don't to add the method in a helper called Data, let's say you add it in the helper Namespace_Modulename_Helper_Something you need the tags tag like this:

<tags helper="modulename/something/getBlockTags" />
1
  • sir. Thank you very much to give quick reply. method still valid with custom code.+1 for me. Commented Jul 22, 2015 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.