0

What I need to do is wrap some structural markup around ALL CMS content. For example:

<div class="section-body">
   <!-- content from editor will be output here -->
</div>

I don't want to actually put this in the template becuase that become somehwat problematic as the content_header needs to actually be outside of this element producing the markup:

<div class="section-header">
  <!-- content_header will be output here or this can actually 
       be part of content_header.phtml -->
</div>

<div class="section-body">
  <!-- content from editor will be output here -->
</div>

<!--possibly the following though currently nothing calls for it -->
<div class="section-footer">
</div>

Essentially what I am trying to do is assign a template to the content region itself, independent of the page without having to have admins maintain some kind of tricky layout xml that needs to be copied and pasted to each CMS page as time goes by.

1

Option 1.
I guess the simplest way is to rewrite the method Mage_Cms_Block_Page::_toHtml(). That's where the content is rendered. You can make it look like this:

protected function _toHtml()
{
    /* @var $helper Mage_Cms_Helper_Data */
    $helper = Mage::helper('cms');
    $processor = $helper->getPageTemplateProcessor();
    //add your wrapper here
    $prefix = '<div class="section-body">';
    $suffix = '</div>';
    $html = $processor->filter($prefix.$this->getPage()->getContent().$suffix);
    $html = $this->getMessagesBlock()->toHtml() . $html;
    return $html;
}

A second option is to rewrite the class Mage_Cms_Model_Page and add a method called getContent that looks like this:

public function getContent() {
    $prefix = '<div class="section-body">';
    $suffix = '</div>';
    return $prefix.$this->getData('content').$suffix;
}

A third option could be to use the event cms_page_render. This seams cleaner but I'm not 100% sure it works.
The observer should receive as a parameter the page object and you can do it like this:

public function changePageContent($observer) {
    $page = $observer->getEvent()->getPage(); 
    $prefix = '<div class="section-body">';
    $suffix = '</div>';
    $page->setContent($prefix.$page->getContent().$suffix);
    return $this;
}

But I have a suggestion. no matter what approach you use don't hardcode the wrapper. Put it in a system->config field. This way you can change it without changing the code and you can have different wrappers for different store views that use different themes.
So in all the options above the variables $prefix and $suffix will look like this:

$prefix = Mage::getStoreConfig('some/path/prefix');
$suffix = Mage::getStoreConfig('some/path/suffix');
2
  • The observer seems like the logical choice assuming it works as intended (i've noticed the hard way that sometimes the evetns actually happen after or before the key operation you want to supplement/modify). I'm going to leave this open until I have a chance to attempt the implementation, jsut in case anyone else has some additional bright ideas that dont require class rewrites :-) +1 Jun 3 '14 at 18:25
  • 2
    @prodigitalson. I've tested the observer approach (3rd one in my answer) and it works fine. So I guess this is the way to go. Simple and non-intrusive.
    – Marius
    Jun 4 '14 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.