如何在 Magento 中将 CSS 文件添加到 CUSTOM WIDGET?

Ale*_*lex 5 javascript css widget magento

所以,我自定义部件Magento的(像幻灯片产品)。

并希望向我的小部件添加自定义CSS / Javascript

我不想每次都显示(加载)CSS / JS 文件。


我只想在 PAGE 包含自定义小部件时加载 JS/CSS。

谢谢。

Erf*_*fan 4

这取决于您是否已将小部件添加为小部件实例(CMS -> 小部件)或将其包含在页面、静态块或电子邮件的内容区域中,如下所示:{{widget type="blabla/carousel"}}

如果您已将其作为小部件实例包含在内,则可以像这样添加资产:

protected function _prepareLayout() {
    if ($head = $this->getLayout()->getBlock('head')) { 
        $head->addCss('myfile.css');
    }
    return parent::_prepareLayout();
}
Run Code Online (Sandbox Code Playgroud)

..在您实现的块中Mage_Widget_Block_Interface

(参见http://www.magentocommerce.com/boards/viewthread/212110/

AFAIK,当您将小部件内联作为模板指令包含时,不可能添加资产。这只是因为 HTML 头已经输出了它的 HTML:模板指令(例如{{widget}}{{var}})在块的 _toHtml 阶段被注意到。在已经渲染的块上调用任何方法都不会产生任何效果。

另请参阅中方法中的$processor->filter调用_toHtmlMage_Cms_Block_Page

protected function _toHtml()
{
    /* @var $helper Mage_Cms_Helper_Data */
    $helper = Mage::helper('cms');
    $processor = $helper->getPageTemplateProcessor();
    $html = $processor->filter($this->getPage()->getContent());
    $html = $this->getMessagesBlock()->toHtml() . $html;
    return $html;
}
Run Code Online (Sandbox Code Playgroud)