Magento根据模块选项来设置不同的模板

by Web全栈工程师 on 2011 年 07 月 11 日

Magento后台可以通过disable/enable来启用和禁用model,同样,我们可以模块的配置选项来设置不同的模板。

Magento Model到调用不同的模板就需要用到block里的”ifconfig”这个参数,

我们可以在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;
            }
        }

        $method = (string)$node['method'];
        if (!empty($node['block'])) {
            $parentName = (string)$node['block'];
        } else {
            $parentName = $parent->getBlockName();
        }

        $_profilerKey = 'BLOCK ACTION: '.$parentName.' -> '.$method;
        Varien_Profiler::start($_profilerKey);

        if (!empty($parentName)) {
            $block = $this->getBlock($parentName);
        }
        if (!empty($block)) {

            $args = (array)$node->children();
            unset($args['@attributes']);

            foreach ($args as $key => $arg) {
                if (($arg instanceof Mage_Core_Model_Layout_Element)) {
                    if (isset($arg['helper'])) {
                        $helperName = explode('/', (string)$arg['helper']);
                        $helperMethod = array_pop($helperName);
                        $helperName = implode('/', $helperName);
                        $arg = $arg->asArray();
                        unset($arg['@']);
                        $args[$key] = call_user_func_array(array(Mage::helper($helperName), $helperMethod), $arg);
                    } else {
                        /**
                         * if there is no helper we hope that this is assoc array
                         */
                        $arr = array();
                        foreach($arg as $subkey => $value) {
                            $arr[(string)$subkey] = $value->asArray();
                        }
                        if (!empty($arr)) {
                            $args[$key] = $arr;
                        }
                    }
                }
            }

            if (isset($node['json'])) {
                $json = explode(' ', (string)$node['json']);
                foreach ($json as $arg) {
                    $args[$arg] = Mage::helper('core')->jsonDecode($args[$arg]);
                }
            }

            $this->_translateLayoutNode($node, $args);
            call_user_func_array(array($block, $method), $args);
        }

        Varien_Profiler::stop($_profilerKey);

        return $this;
    }

如果magento config的值为真,ifconfig不会调用相关的模板文件,

如果需要自定义的话,可以在_generateAction 加入自己的判断条件,

以下这个例子可以作为参考:

在模块的system.xml里设置特殊的enable

<config>
    <sections>
        <advanced>
            <groups>
                <YOURMODULE>
                    <fields>
                        <enable>
                            <label>YOUR MODULE</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_enabledisable</source_model>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </enable>
                    </fields>
                </YOURMODULE>
            </groups>
        </advanced>
    </sections>
</config>

这样,你可以在layout 文件里加入模板选择代码了:

<action method="setTemplate" ifconfig="advanced/YOURMODULE/enable">
    <template>mytemplate.phtml</template>
</action>

 

 

 

Comments on this entry are closed.

Previous post:

Next post: