使用 before/after 放置块的 Magento XML 几乎不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14484955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magento XML using before/after to place blocks hardly ever works
提问by mike-source
I'm a front-end Magento dev, have built quite a few of my own themes and I want to understand Magento's XML block positioning better...
我是前端 Magento 开发人员,已经构建了很多我自己的主题,我想更好地了解 Magento 的 XML 块定位...
I normally use a local.xmlfile to manipulate everything, I can define a block as follows:
我通常使用一个local.xml文件来操作一切,我可以如下定义一个块:
<cms_index_index>
<reference name="root">
<block type="core/template" name="example_block" as="exampleBlock" template="page/html/example-block.phtml"/>
</reference>
</cms_index_index>
This would create a block on the home page (cms_index_index) and since the block is created one level under root, I would normally call the block by adding:
这将在主页 ( cms_index_index)上创建一个块,并且由于该块是在 下一级创建的root,我通常会通过添加以下内容来调用该块:
<?php echo $this->getChildHtml('exampleBlock') ?>
...to 1column.phtml(or 2columns-left/right.phtml, 3columns.phtmletc). The block can be placed on any page by substituting cms_index_indexfor the appropriate page tag.
......到1column.phtml(或2columns-left/ right.phtml,3columns.phtml等等)。通过替换cms_index_index适当的页面标签,块可以放置在任何页面上。
I see stuff like the following throughout the core XML files, and in tutorials:
我在整个核心 XML 文件和教程中看到如下内容:
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
contentis a block which is part of magento's general page structure and, from what I understand, before="content"should place it where you'd expect, without needing to use getChildHtml('exampleBlock'), so far so good... however, before/after hardly ever seems to work for me, and I frequently find myself resorting to the getChildHtml method as backup, which isn't always ideal, and means editing more .phtml files than necessary.
content是一个块,它是 magento 的一般页面结构的一部分,据我所知,before="content"应该把它放在你期望的地方,不需要使用getChildHtml('exampleBlock'),到目前为止这么好......但是,之前/之后似乎几乎没有工作我和我经常发现自己求助于 getChildHtml 方法作为备份,这并不总是理想的,并且意味着编辑比必要更多的 .phtml 文件。
I've tried:
我试过了:
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
Nothing appears...
什么都没有出现...
<reference name="root">
<block type="core/template" name="example_block" after="header" template="page/html/example-block.phtml"/>
</reference>
Still nothing.... I'm also aware of using before="-"or after="-"to place something before everything within it's parent block. I occasionally have some luck with that, but generally just get confused and frustrated.
仍然什么都没有......我也知道在它的父块中的所有东西之前使用before="-"或after="-"放置一些东西。我偶尔会有一些运气,但通常只是感到困惑和沮丧。
I've googled all over the place for 'magento xml before/after not working' and beginning to wonder if its just me this happens to... can anyone explain when I can and can't use before/after to position blocks? What's wrong with the above examples?
我在到处搜索“magento xml 之前/之后不工作”并开始怀疑它是否只是我这种情况发生......谁能解释我何时可以和不能使用之前/之后定位块?上面的例子有什么问题?
I'm in magento 1.7.0.2 (latest available at time of posting)
我在 magento 1.7.0.2(发布时最新可用)
The main motivation for this is to reduce the number of core .phtml files I need to edit just to add a getChildHtml(), so if there is another (XML) way to get around this I'd be interested to know...
这样做的主要动机是减少我需要编辑的核心 .phtml 文件的数量来添加一个getChildHtml(),所以如果有另一种(XML)方法来解决这个问题,我很想知道......
回答by Alan Storm
The beforeand afterattributes only work in one of two cases:
在before和after属性只有在两种情况之一的工作:
- When you insert into a
core/text_listblock - When your template block calls
getChildHtmlwithout any parameters
- 插入
core/text_list块时 - 当您的模板块在
getChildHtml没有任何参数的情况下调用时
When you say
当你说
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
you're telling Magento
你在告诉 Magento
Hey Magento, put the
example_blockinside therootblock.
嘿 Magento,把
example_block里面的root块。
When you put a number of different blocks inside a parent, those blocks have an implicit order. For template blocks, this order doesn't matter, since those blocks are being explicitly rendered.
当您将多个不同的块放入父级时,这些块具有隐式顺序。对于模板块,此顺序无关紧要,因为这些块是显式呈现的。
<?php echo $this->getChildHtml('example_block') ?>
However, there's two cases where order matters. First, if you call
但是,有两种情况下顺序很重要。首先,如果你打电话
<?php echo $this->getChildHtml() ?>
from a template, then Magento will render allthe child blocks, in order.
从模板中,然后 Magento 将按顺序渲染所有子块。
Secondly, there's a special type of block called a "text list" (core/text_list/Mage_Core_Block_Text_List). These blocks render all their children automatically, again in order. The contentblock is an example of this
其次,有一种特殊类型的块,称为“文本列表”(core/text_list/ Mage_Core_Block_Text_List)。这些块再次按顺序自动渲染所有子项。该content块是一个例子
<block type="core/text_list" name="content"/>
That's why you can insert blocks into contentand they render automatically.
这就是为什么您可以将块插入其中content并自动渲染。
So, in your example above, you're inserting blocks into the rootblock. The rootblock is a template block whose phtml template uses getChildHtmlcalls with explicit parameters. Therefore the beforeand afterattributes don't do what you (and many others, including me) wish they did.
因此,在上面的示例中,您将块插入到root块中。该root块是一个模板块,其 phtml 模板使用getChildHtml带有显式参数的调用。因此before和after属性不会做你(和许多其他人,包括我)希望他们做的事情。

