XML tag to add array in cache tag for layout file
-
13-12-2019 - |
Question
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.
Solution
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>
OTHER TIPS
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" />