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

问题描述

我正在尝试向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)中注册事件观察器:

<?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布局文件以使用自定义布局句柄在某些地区:

<?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:将内容块添加到结构块"content"的末尾.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:29