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

查看:54
本文介绍了在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;
}

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

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天全站免登陆