dongshi1914 2016-01-06 15:45
浏览 48

Magento - 删除空P标签模块

I'm trying to remove empty P tags within Magento CMS.

I've been following a couple of tutorials and have tried to combine the information in both, but it isn't working and wondered if anyone could point out an error in my code.

http://gielberkers.com/magento-remove-wrapping-paragraph-around-widget/ https://www.smashingmagazine.com/2012/03/basics-creating-magento-module/

I've correctly activated my module.

My module is located at... app / code / local / Rapport Design / RemoveEmptyPTags

The code for my config.xml file located in RemoveEmptyPTags / etc is...

<!--
    The module's node contains basic
    information about each Magento module
-->
<modules>

    <!--
        This must exactly match the namespace and module's folder
        names, with directory separators replaced by underscores
    -->
    <RapportDesign_RemoveEmptyPTags>

        <!-- The version of our module, starting at 0.0.1 -->
        <version>0.0.1</version>

    </RapportDesign_RemoveEmptyPTags>

</modules>

<!-- Configure our module's behavior in the global scope -->
<frontend>


    <!-- Defining models -->
    <models>

        <!--
            Unique identifier in the model's node.
            By convention, we put the module's name in lowercase.
        -->
        <rapportdesign_removeemptyptags>

            <!--
                The path to our models directory, with directory
                separators replaced by underscores
            -->
            <class>RapportDesign_RemoveEmptyPTags_Model</class>

        </rapportdesign_removeemptyptags>

    </models>

    <!-- Defining an event observer -->
    <events>

        <!-- The code of the event we want to observe -->
        <cms_page_render>

            <!-- Defining an observer for this event -->
            <observers>

                <!--
                    Unique identifier within the
                    catalog_product_save_after node.
                    By convention, we write the module's
                    name in lowercase.
                -->
                <rapportdesign_removeemptyptags>

                    <!-- The model to be instantiated -->
                    <class>rapportdesign_removeemptyptags/observer</class>

                    <!-- The method of the class to be called -->
                    <method>cmsPageRenderEvent</method>

                    <!-- The type of class to instantiate -->
                    <type>singleton</type>

                </rapportdesign_removeemptyptags>

            </observers>

        </cms_page_render>

    </events>

</frontend>

The code for my Observer.php file located in RemoveEmptyPTags / Model is...

class RapportDesign_RemoveEmptyPTags_Model_Observer {

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

$content = Mage::helper('RapportDesign_RemoveEmptyPTags')->processContent($content);

$page->setContent($content);
}

}

The code for my Data.php file located in RemoveEmptyPTags / Helper is...

class RapportDesign_RemoveEmptyPTags {

public function processContent($content)
{
// Remove wrapping paragraphs around widgets:
$content = preg_replace('/\<p\>{{(.*?)}}\<\/p\>/', '{{$1}}', $content);

// Remove div around widgets
$content = preg_replace('/\<div\>{{(.*?)}}\<\/div\>/', '{{$1}}', $content);

// Remove empty paragraphs:
$content = preg_replace('/<p>(|\s*|&nbsp;|
)<\/p>/', '', $content);

return $content;
}

}

If anyone can see anything obviously wrong with this, it would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanqing2209 2016-01-06 17:29
    关注

    Well I've not got the above code working, but I have discovered a much easier way to achieve my required results using jQuery...

       $('p').each(function() {
           var $this = $(this);
           if($this.html().replace(/\s|&nbsp;/g, '').length == 0)
               $this.remove();
       });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测