Magento static blocks. Remove wrapping

2019-04-09 14:40发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

edit js/mage/adminhtml/wysiwyg/tiny_mce/setup.js

var settings = {
        mode : (mode != undefined ? mode : 'none'),
        elements : this.id,
        theme : 'advanced',
        plugins : plugins,
        theme_advanced_buttons1 : magentoPlugins + 'magentowidget,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
        theme_advanced_buttons2 : 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,forecolor,backcolor',
        theme_advanced_buttons3 : 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,ltr,rtl,|,fullscreen',
        theme_advanced_buttons4 : 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak',
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left',
        theme_advanced_statusbar_location : 'bottom',
        theme_advanced_resizing : true,
        convert_urls : false,
        relative_urls : false,



        forced_root_block : '', /* <-- Add this setting */



        content_css: this.config.content_css,
        custom_popup_css: this.config.popup_css,
        magentowidget_url: this.config.widget_window_url,
        magentoPluginsOptions: magentoPluginsOptions,


回答4:

This depends on where you are using your static blocks and what templates/theme you are using. Turn on the developer frontend hints, load your offending pages, identify the template file used and then pull out the <p> tabs from your phtml files.

A directly called static block does not put extra <p> tags in.