MBozic
MBozic

Reputation: 1192

Magento CMS page setup Dynamic Meta Data

How to set up meta data (description, title, keywords) for cms page dynamicaly? Meta data should be generated depending of the params in the URL (example: ?part=light).

I've tried placing {{block type="myblock"}} in the keywords of the Meta Data tab but this doesn't evaluate.

I can put this {{block type="myblock"}} in Content and it calls my block when the cms page is displayed. From the block I can check what the URL is and based on that create meta data, but I don't know how to access head block of the CMS page from my block.

Upvotes: 1

Views: 1979

Answers (1)

benmarks
benmarks

Reputation: 23205

The CMS template directives are processed via the template filter (specified in Mage_Cms config.xml), and it's only for the cms/page block. See Mage_Core_Model_Email_Template_Filter (not a typo).

There are so many ways to do this. One possible way would be to observe cms_page_load_after event. In your event observer method you'll do something like the following:

if(Mage::app()->getRequest()->getParam('your_param_etc'))
{
    $observer->getObject()->setTitle('...')
                          ->setKeywords('...')
                          ->setDescription('...');
}

I'm not the biggest fan of this approach though because it ties the request directly to the model. It might be more appropriate to handle the logic via an event observer on controller_action_layout_render_before_cms_page_view and do the following instead:

if(Mage::app()->getRequest()->getParam('your_param_etc'))
{
    $head = Mage::app()->getLayout()->getBlock('head');

    if($head){
        $head->setTitle('...')
             ->setKeywords('...')
             ->setDescription('...');
    }
}

My preference for the latter approach is that this event is triggered in the controller action, which is more logically connected with the request object. Either approach will do the trick.

Upvotes: 2

Related Questions