且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento - xml 布局,为 ifconfig 指定值?

更新时间:2023-11-29 23:30:04

这很适合 某事(自链接)我一直在努力.

This fits in well with something (self-link) I've been working on.

如果没有类重写来改变 ifconfig 的行为,你就不能完全做你想做的事.这是实现 ifconfig 功能的代码.

You can't do exactly what you want without a class rewrite to change the behavior of ifconfig. Here's the code that implements the ifconfig feature.

File: app/code/core/Mage/Core/Model/Layout.php
protected function _generateAction($node, $parent)
{
    if (isset($node['ifconfig']) && ($configPath = (string)$node['ifconfig'])) {
        if (!Mage::getStoreConfigFlag($configPath)) {
            return $this;
        }
    }

如果检测到 ifconfig 的存在并且 config 值返回 true,则不会调用 action 方法.您可以重写 _generateAction 并实现自己的条件,但是随后维护重写的标准负担就落在了您身上.

If the presence of an ifconfig is detected and the config value returns true, the action method will not be called. You could rewrite _generateAction and implement your own conditional, but then the standard burdens of maintaining a rewrite fall you on.

更好的方法是在动作参数中使用辅助方法.类似这样的

A better approach would be to use a helper method in your action paramater. Something like this

<action method="setTemplate">
    <template helper="mymodule/myhelper/switchTemplateIf"/>
</action>

将使用调用结果调用 setTemplate

will call setTemplate with the results of a call to

Mage::helper('mymodule/myhelper')->switchTemplateIf();

switchTemplateIf 中实现您的自定义逻辑,要么保留模板,要么更改它,然后一切顺利.

Implement your custom logic in switchTemplateIf that either keeps the template or changes it and you'll be good to go.