Magento add your own CMS Directive to include your custom template tag

Posted by Damodar Bashyal on July 15, 2014

 

This post details on how I was able to add new template tag on Magento Cms pages and static blocks. If you want to know more about these directives and template tags, you can check official magento wiki here.

I have already added this functionality and example on my github repository here https://github.com/dbashyal/Magento-General-Module-Extension. This module consists of other functionalities, so check code and strip out that you don't need it.

So, Let's go through the files that I have added/updated to make this happen.

First on Tgeneral/etc/config.xml we need to tell magento that we want to add our new filter by adding this block of code:

    <cms>
        <page>
            <tempate_filter>tgeneral/template_filter</tempate_filter>
        </page>
        <block>
            <tempate_filter>tgeneral/template_filter</tempate_filter>
        </block>
    </cms>

Then we need to create that filter at: Tgeneral/Model/Template/Filter.php.

Here we are going to add new Directive. We need to create function name as {{tagThatWeAreGoingToUse}}Directive. As we are going to use {{tgeneral}}, the function will be:

public function tgeneralDirective($construction){}

Here we can extract all the parameters passed through that template variable using Magento function _getIncludeParameters. Once we have all the parameters, we can do whatever we need to do and replace new template tag in cms pages and static blocks with the result.

My requirement was to add alternate store url on multi store magento community site, so I added new helper and updated directive accordingly. First get your current magento store data and then add switch to load alternate store url as:

    public function getAlternateStoreUrl(){
        $store = $this->getStoreData()->getData('code');
        /* @var $model Mage_Catalog_Model_Convert_Adapter_Product */
        $model = Mage::getSingleton('catalog/convert_adapter_product');
        $params['_store_to_url'] = false;
        $params['_nosid'] = true;
        switch($store){
            case 'bookclub':
                return Mage::getModel('core/url')->setStore($model->getStoreByCode('default'))->getUrl('', $params);
                break;
            default:
                return Mage::getModel('core/url')->setStore($model->getStoreByCode('bookclub'))->getUrl('', $params);
        }
    }

Now you can add new custom magento template tag as: {{tgeneral helper="getAlternateStoreUrl"}}

e.g.

    <div class="footer-logo">
        <a href="{{tgeneral helper="getAlternateStoreUrl"}}">
            {{widget type="cms/widget_block" template="cms/widget/static_block/default.phtml" block_id="54"}}
        </a>
    </div>

For full code, download tgeneral module here - https://github.com/dbashyal/Magento-General-Module-Extension

 
not published on website


QR Code: Magento add your own CMS Directive to include your custom template tag