开发者

Add css from a block

开发者 https://www.devze.com 2023-03-31 23:07 出处:网络
I am building a custom magento module and i try to add a custom css file to my block. I wrote : <?php

I am building a custom magento module and i try to add a custom css file to my block. I wrote :

<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
   protected functi开发者_JAVA百科on _prepareLayout()
   {
      $this->getLayout()->getBlock('head')->addCss('css/mycompany/mymodule/stylesheet.css');
      return parent::_prepareLayout();
   }

 }

but it doesn't work, my css file is not loaded, any idea? Thank you.


My alternative solution was to add this in my xml layout :

<default>
  <reference name="head">
      <action method="addCss"><stylesheet>css/interactiveslider.css</stylesheet></action>
  </reference>
</default>

Thank you for your help


All the CSS & Images are normally available in the "skin" folder. It should be:-

"skin" folder
   -> Package Name (like "base" or "default")
      -> Theme Name (like "modern" or "mycompany")
         -> "css" folder
            -> "mymodule" folder
               -> "stylesheet.css" file

So I suppose that you have been following this above-mentioned basic structure, which is considered as one of the best practices.

Coming back to your question, I suppose that you have mentioned the correct block class in your module's layout file "layout.xml". So the above code should be, according to the above folder structure:-

<?php
class Wise_InteractiveSlider_Block_Slider extends Mage_Core_Block_Template
{
   protected function _prepareLayout()
   {
      $this->getLayout()->getBlock('head')->addCss('css/mymodule/stylesheet.css');
      return parent::_prepareLayout();
   }
}

Lastly, please make sure that you have uploaded your CSS file "stylesheet.css" in the correct folder.

Hope it helps.


You can only use the _prepareLayout() method if the block is defined in the layout XML. If you 'inline' the block inside a CMS page via the {{block type... method, the layout is already prepared by the time the block is rendered

0

精彩评论

暂无评论...
验证码 换一张
取 消