动态插入Magento的布局xml

Lov*_*aur 1 magento

我是Magento的新手.我试图建立一个模块,在渲染之前动态地将代码xml插入到布局xml中 - 类似于CMS>页面的构建方式.

就像我们如何在页面的设计部分(admin> cms> page)中指定layout xml一样,我想通过我的模块插入layout.xml.

基本上,我想

  • 有一个管理部分,我可以通过表格输入布局代码并存储在数据库中 - 我已经找到了这一部分
  • 让Magento将这些代码片段存储在数据库中,并在聚合和解释布局文件之前创建一个xml文件. - 我无法建立这个部分.

任何帮助,将不胜感激.

iva*_*dja 5

只需要一点启发您可以使用观察者添加这些布局xml,假设您希望在生成xml之前添加这些布局xml

我们可以使用活动 controller_action_layout_generate_xml_before

这是示例代码(in config.xml)

<frontend>
    <events>
        <controller_action_layout_generate_xml_before>
            <observers>
                <add_new_layout>
                    <class>test/observer</class>
                    <method>addNewLayout</method>
                </add_new_layout>
            </observers>
        </controller_action_layout_generate_xml_before>
    </events>
</frontend>
Run Code Online (Sandbox Code Playgroud)

这是 Observer.php

public function addNewLayout($observer){
    $layout = $observer->getEvent()->getLayout();
    $update = $layout->getUpdate();

    //$action = $observer->getEvent()->getAction();
    //$fullActionName = $action->getFullActionName();
    //in case you're going to add some conditional (apply these new layout xml on these action or other things, you can modify it by yourself)

    //here is the pieces of layout xml you're going to load (you get it from database)
    $xml = "<reference name='root'><remove name='footer'></remove></reference>";
    $update->addUpdate($xml);

    return;
}
Run Code Online (Sandbox Code Playgroud)