在 magento 事件观察器中以编程方式更新布局 [英] update layout programmatically in magento event observer

查看:20
本文介绍了在 magento 事件观察器中以编程方式更新布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改产品详细信息页面的块 (product.info) 的模板 (view.phtml),为此,我正在观察一个事件 (controller_action_layout_generate_blocks_before),在进行必要的检查后,我试图通过以下方式更改块的模板 (product.info):

I am trying to change the template(view.phtml) of a block (product.info) for product detail page, to do this, I am observing an event (controller_action_layout_generate_blocks_before), in it after making necessary checks I am trying to change the template of the block (product.info) in following way:

$layout = $observer->getEvent()->getLayout();
$layout->getUpdate()->addUpdate('
        <reference name="product.info">
            <action method="setTemplate">
                <template>customlayout/product/view.phtml</template>
            </action>                                                          
        </reference>');
$layout->getUpdate()->load();
$layout->generateXml();

如果我输入 "<remove name='product.info'/>" ,它将被删除,但在尝试执行上述操作时,它不起作用.

要求是将模板(产品详细信息)动态切换到针对当前产品的选定模板(在 CustomModule 中).

If I put "<remove name='product.info'/>" , it will be removed but when trying to do the above, its not working.

Requirement is to switch the template (product detail) dynamically to the selected one (in CustomModule) against the current product.

推荐答案

正如 Ben 所说,我不知道你为什么要把它放在观察者身上,但你的问题是 的顺序loadLayout.

As Ben said, I don't know why you're going to put it on the observer but the problem in your case is the sequence of loadLayout.

您可以使用以下方法检查加载的布局 xml:

You can check your loaded layout xml by using:

Mage::log(Mage::getSingleton('core/layout')->getUpdate()->asString());

很确定您的 <action method="setTemplate"><template>customelayout/product/view.phtml</template> 已被其他 setTemplate 覆盖> 这就是您的模板未显示的原因.

Pretty sure your <action method="setTemplate"><template>customelayout/product/view.phtml</template> has been overridden by other setTemplate that's the reason your template is not shown.

Mage_Core_Controller_Varien_Action

public function loadLayout($handles=null, $generateBlocks=true, $generateXml=true)
{
    // if handles were specified in arguments load them first
    if (false!==$handles && ''!==$handles) {
        $this->getLayout()->getUpdate()->addHandle($handles ? $handles : 'default');
    }

    // add default layout handles for this action
    $this->addActionLayoutHandles();

    $this->loadLayoutUpdates(); //in here: $this->getLayout()->getUpdate()->load();

    if (!$generateXml) {
        return $this;
    }
    //event: controller_action_layout_generate_xml_before
    $this->generateLayoutXml(); //in here: $this->getLayout()->generateXml();

    if (!$generateBlocks) {
        return $this;
    }
    //event: controller_action_layout_generate_blocks_before, your observer is located here
    $this->generateLayoutBlocks(); //in here: $this->getLayout()->generateBlocks();
    $this->_isLayoutLoaded = true;

    return $this;
}

因此,您将使用事件修改 xml:controller_action_layout_generate_blocks_before.

So, you're going to modify the xml using event: controller_action_layout_generate_blocks_before.

这意味着你需要做的是:

It means what you need to do is:

//add the update
$layout->getUpdate()->addUpdate('<reference name="product.info"><action method="setTemplate"><template>customelayout/product/view.phtml</template></action></reference>');
//then generate the xml
$layout->generateXml();

导致您出现问题的原因是:

What cause your problem is:

$layout->getUpdate()->load();

之后又被调用了

$layout->getUpdate()->addUpdate('<reference name="product.info"><action method="setTemplate"><template>customelayout/product/view.phtml</template></action></reference>');

虽然最好使用事件:controller_action_layout_generate_xml_before.这样您就不需要两次生成 xml.

Though it is better to use event: controller_action_layout_generate_xml_before. So that you don't need to generate your xml twice.

这篇关于在 magento 事件观察器中以编程方式更新布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆