Magento:在结构块“内容"的末尾添加内容块; [英] Magento: Add content block at the end of the structual block "content"

查看:27
本文介绍了Magento:在结构块“内容"的末尾添加内容块;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向 Magento 添加一个内容块,它应该在主要内容下方的每一侧都可见.我想用一个自定义扩展来归档它,所以我可以复制这个扩展并且它在不接触核心设计文件的情况下工作.我的扩展程序包括以下布局更新:

I'm trying to add a content block to Magento, which should be visible on every side below the main content. I want to archive this with a custom extension, so I can copy this extension and it workes without touching core design files. My extension includes the following layout update:

<default>
    <reference name="content">
        <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
    </reference>
</default>

我的问题是,属性 after="-" 不起作用.该块始终显示在内容块的顶部.似乎 beforeafter 没有任何影响.如果我将块移动到页脚,属性 beforeafter 工作正常.

My problem is, that the attribute after="-" is not working. The block always showes up at the top of the content block. Seems before and after have no consequence. If I move the block to i.e. footer, the attributes before and after are working fine.

如何将我的块放在块内容"的底部

How can I place my block at the bottom of block "content"

推荐答案

据我所知,问题是您在默认"布局句柄中指定了块,而内容"块中的大部分内容都是由稍后应用的其他布局句柄添加.这就是为什么在您的 XML 注册文件中添加的依赖项(由 Fabian 提到)没有帮助.

As far as I can see the problem is that you specify your block in the "default" layout handle while most of the content in the "content" block is added by other layout handles which are applied later. That's why the added dependencies in your XML registration file (mentioned by Fabian) are not helping.

请根据您的需要考虑这两个选项:

Please consider these two options depending on your needs:

在您的 XML 布局文件(local.xml 或自定义文件)中,添加一个新的布局句柄:

In your XML layout file (local.xml or a custom one), add a new layout handle:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">

    <!-- your other adjustments for default, category_product_view and so on go here -->

    <add_my_block>
        <reference name="content">
            <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
        </reference>
    </add_my_block>
</layout>

现在您创建一个事件观察器来将您的布局句柄注入到您的布局中:

Now you create an event observer to inject your layout handle into your layout:

    <?php

    class YourCompany_YourExtension_Model_Observer
    {
        /**
         * Adds a block at the end of the content block.
         * 
         * Uses the event 'controller_action_layout_load_before'.
         * 
         * @param Varien_Event_Observer $observer
         * @return YourCompany_YourExtension_Model_Observer
         */
        public function addBlockAtEndOfMainContent(Varien_Event_Observer $observer)
        {
            $layout = $observer->getEvent()->getLayout()->getUpdate();
            $layout->addHandle('add_my_block');
            return $this;
        }
    }

然后在您的 XML 扩展配置文件 (config.xml) 中注册事件观察者:

Then you register the event observer in your XML extension configuration file (config.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<config>
    <modules>
        <YourCompany_YourExtension>
            <version>0.0.1</version>
        </YourCompany_YourExtension>
    </modules>

    <frontend>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <mymod_add_block_at_end_of_main_content>
                        <type>singleton</type>
                        <class>mymod/observer</class>
                        <method>addBlockAtEndOfMainContent</method>
                    </mymod_add_block_at_end_of_main_content>
                </observers>
            </controller_action_layout_load_before>
        </events>
        <!-- declaring your layout xml etc. -->
    </frontend>

    <global>
        <!-- declaring your block classes etc. -->
        <models>
            <mymod>
                <class>YourCompany_YourExtension_Model</class>
            </mymod>
        </models>
    </global>
</config>

现在您的块应该在其他块下方.我对主页、客户登录页面和类别查看页面进行了成功测试.如果您必须在几页上排除您的块,您可以检查您的事件观察器是否应在该特定页面上排除该块.

Now your block should end up below the other blocks. I tested this successfully for the homepage, customer login page and category view page. If you have to exclude your block on a few pages, you can check in your event observer if the block should be excluded on that certain page.

向您的 XML 布局文件添加布局句柄,就像我们之前所做的那样,但不是创建和注册事件观察器,只需告诉您的 XML 布局文件使用自定义布局句柄 在某些领域:

Add a layout handle to your XML layout file just as we did before but instead of creating and registering an event observer, just tell your XML layout file to use the custom layout handle in some areas:

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">

    <catalog_category_default>
        <update handle="add_my_block" />
    </catalog_category_default>

    <catalog_category_layered>
        <update handle="add_my_block" />
    </catalog_category_layered>

    <cms_page>
        <update handle="add_my_block" />
    </cms_page>

    <!-- and so on -->

    <add_my_block>
        <reference name="content">
            <block type="mymod/blockname" name="myblockname" after="-" template="mymod/block.phtml"/>
        </reference>
    </add_my_block>

</layout>

这篇关于Magento:在结构块“内容"的末尾添加内容块;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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