添加JS& CSS通过观察者管理头部在magento

时间:2016-07-17 07:29:28

标签: javascript css xml magento observers

我需要添加内联JS,JS路径和&基于设置表单数据库的CSS路径/内联到我的magento管理员的头部,因此只有我能想到的选项是使用观察者来加载js / css。

我在控制器和xml级别尝试了各种技术,如下所示;

Adminhtml控制器

//Does NOT append to head
$block = $this->getLayout()->createBlock('Mage_Core_Block_Template','my.block', array('template' => 'my.file.phtml'));
$this->getLayout()->getBlock('head')->append($block);

XML布局文件

<reference name="head">
<!-- Does NOT work -->
<block type="core/template" name="mytemplate" as="mytemplate" template="add.to.head.phtml"/>

<!-- Works but at End of page/footer, I need it in the head -->
<block type="core/template" output="toHtml" name="mytemplate" as="mytemplate" template="add.to.head.phtml"/>
</reference>

通过观察员

config.xml中

<global>
...
<adminhtml_controller_action_layout_generate_xml_before>
        <observers>
            <add_stufftohead>
            <type>singleton</type>
                <class>myextension/adminobserver</class>
                <method>addJavascriptBlock</method>
            </add_stufftohead>
        </observers>
</adminhtml_controller_action_layout_generate_xml_before>
...
</global>

Adminobserver.php

//Does NOT work either
public function addJavascriptBlock($observer)
    {

    $js_core_output = "<![CDATA[<script type='text/javascript' >alert('Me works!!!!');</script>]]>";
    //Output to XML
    $layout = $observer->getEvent()->getLayout();
    $update = $layout->getUpdate();
    $xml = "<reference name='head'><block type='core/text' name='add.to.head'><action method='setText'><text>
    $js_core_output
    </text></action></block></reference>";
    $update->addUpdate($xml);
    return;

   }

似乎很难添加到magento管理头,而不使用addJS覆盖head.phtml的addCSS,我不会这样做..

0 个答案:

没有答案