【问题标题】:How can I change a template via config.xml of custom module in Magento?如何通过 Magento 中自定义模块的 config.xml 更改模板?
【发布时间】:2012-08-18 17:36:56
【问题描述】:

我模块的 config.xml 的这个内容。

我想通过这个模块更改单页模板文件。

如果我更改 onepage.xml 中的代码,它是有效的,但我想用我的模块来做这个。

这段代码有什么问题?

<config>
    <modules>
        <Mynamespace_Mymodule>
            <version>1.0</version> 
        </Mynamespace_Mymodule>
    </modules>
    <frontend>
        <layout>
            <checkout_onepage_index>
                <reference name="checkout.onepage">
                    <action method="setTemplate"><template>mynamespace/mymodule/onepage.phtml</template></action>
                </reference>
            </checkout_onepage_index>
        </layout>
    </frontend>
</config>

【问题讨论】:

    标签: xml magento module


    【解决方案1】:

    你不能从 config.xml 调用布局

    你说:我在 onepage.xml 中更改代码 -> 看起来你的意思是 checkout.xml 这是定义术语。我们可能对模块有不同的定义。

    假设您在app/code/local/[Mynamespace]/[Mymodule] 中有模块 您的[Mymodule] 正在呼叫mymodule.xml 你说它是从你的模块中调用的吗?

    1) 如果是,那么您可以在 config.xml 中使用此代码

    <config>
        <modules>
            <Mynamespace_Mymodule>
                <version>1.0</version> 
            </Mynamespace_Mymodule>
        </modules>
        <frontend>
            <layout>
                <updates>
                    <mymodule>
                        <file>mymodule.xml</file>
                    </mymodule>
                </updates>
            </layout>
        </frontend>
    </config>
    

    该代码将调用名为 mymodule.xml 的布局 然后在app/design/frontend/[base/default]/[default/yourtheme]/layout/mymodule.xml中创建布局文件

    <?xml version="1.0"?>
    <layout version="0.1.0">
        <checkout_onepage_index>
            <reference name="checkout.onepage">
                <action method="setTemplate"><template>mynamespace/mymodule/onepage.phtml</template></action>
            </reference>
        </checkout_onepage_index>
    </layout>
    

    2) 如果没有 -> 你的模块定义只有app/code/local/[Mynamespace]/[Mymodule]下的文件,那么你需要重写Onepage的块

    config.xml

    <config>
        <modules>
            <Mynamespace_Mymodule>
                <version>1.0</version> 
            </Mynamespace_Mymodule>
        </modules>
        <global>
            <blocks>
                <checkout>
                    <rewrite>
                        <onepage>Mynamespace_Mymodule_Block_Checkout_Onepage</onepage>
                    </rewrite>
                </checkout>
            </blocks>
        </global>
    </config>
    

    使用该配置,您的Mage_Checkout_Block_Onepage 将被Mynamespace_Mymodule_Block_Checkout_Onepage 重写(只要保持目录结构匹配,您就可以更改名称)。

    例如,您的文件将被放入:app/code/local/[Mynamespace]/[Mymodule]/Block/Checkout/Onepage.php

    最后你的app/code/local/[Mynamespace]/[Mymodule]/Block/Checkout/Onepage.php 会是这样的:

    class Mynamespace_Mymodule_Block_Checkout_Onepage extends Mage_Checkout_Block_Onepage
    {
        public function __construct()
        {
            parent::__construct();
            $this->setTemplate('mynamespace/mymodule/onepage.phtml');
        }
    }
    

    【讨论】:

    • 非常感谢!有用。我需要的第一个理想。祝你有美好的一天^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多