Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento static blocks. Remove wrapping <p>

When i create static block magento wraps content with <p> tags. Which is very bad for DOM. Is is possible to remove it somehow. I suppose it is some javascript but i don't know which one.

like image 921
Jevgeni Smirnov Avatar asked Jun 14 '11 15:06

Jevgeni Smirnov


2 Answers

Actually wrong on my earlier answer.

You need to turn the static block WYSIWYG editor off-by-default.

Go to System -> Configuration, find General section on left hand side, click on Content Management and set 'Enable WYSIWYG Editor' to 'Disable by default' from list.

Then edit your static blocks carefully - use the WYSIWYG but check your HTML afterwards.

This behaviour is a standard feature of WYSIWYG editors, that is what they are for, the <p> tags are added in because they make nicely formatted text. Clearly this is not what you want if you add a static block containing just an image, so step out of the editor and check for <p> tags.

The WYSIWYG editor can also mangle variables entered into the static blocks, and it slows down admin page load times, therefore it is best to have it turned off by default.

like image 73
ʍǝɥʇɐɯ Avatar answered Oct 20 '22 18:10

ʍǝɥʇɐɯ


A more user-friendly method would be to catch the cms_page_render-event, and use a regular expression to 'unwrap' the widget:

config:

<cms_page_render>
    <observers>
        <your_unique_handler>
            <type>singleton</type>
            <class>Package_Module_Model_Observer</class>
            <method>cmsPageRenderEvent</method>
        </your_unique_handler>
    </observers>
</cms_page_render>

observer:

public function cmsPageRenderEvent($observer)
{
    /* @var $page Mage_Cms_Model_Page*/
    $page = $observer->getPage();

    // Remove wrapping paragraphs around widgets:
    $content = $page->getContent();
    $content = preg_replace('/\<p\>{{(.*?)}}\<\/p\>/', '{{$1}}', $content);
    $page->setContent($content);
}

This would unwrap the widget out of their paragraphs prior to Magento's execution of them.

Edit: the part between {{ and }} should be non-greedy.

like image 35
Giel Berkers Avatar answered Oct 20 '22 19:10

Giel Berkers