Monday, 8 August 2011

Passing Paramters to Magento CMS Static Blocks

If we want our magento controllers to be able to load a static cms block (using AJAX for instance) and render the variable values that it can pass when we load the block then we can, Load the block and declare an array of variables; $block = $this->getLayout()->createBlock('cms/block')->setBlockId('block_id'); $variables = array(); Declare the variables we wish to pass (that we might have got from the params passed on to controller)... so we set the variable name and value as, $variables['variable_name'] = $value; Now when we return the html or render it we need to pass it through a filter, which has various directives method to run the codes in the parenthesis, {{ }}, within a static block, Therefore we render it now as, $filter = Mage::getModel('core/email_template_filter'); //or any of the $filter->setVariables($variables); echo $filter->filter($block->toHtml()); So now in the cms block anything with {{ var variable_name }} will be parsed. Therefore we can send variables and parameters to magento cms static block in this fashion even when we are rendering it using AJAX.