2016-01-06 78 views
0

我試圖Magento的CMS內取出空P-標記。Magento的 - 刪除空P-標籤模塊

我一直在下面幾個教程,並試圖將信息在這兩個結合起來,但它不工作,並想知道如果有人能在我的代碼指出錯誤。

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

我已經正確激活了我的模塊。

我模塊位於... 應用程序/代碼/本地/ Rapport的設計/ RemoveEmptyPTags

我的config.xml文件位於RemoveEmptyPTags /代碼等是...

<!-- 
    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> 

的代碼我位於RemoveEmptyPTags /型號Observer.php文件...

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); 
} 

} 

位於RemoveEmptyPTags /幫手我Data.php文件的代碼是...

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;|\n)<\/p>/', '', $content); 

return $content; 
} 

} 

如果任何人都可以明顯看到什麼錯,這將不勝感激。

回答

1

嗯,我還沒有得到上面的代碼工作,但我發現了一個更簡單的方法使用jQuery實現我需要的結果......

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