Magento XML 使用 before/after 放置块几乎不起作用 [英] Magento XML using before/after to place blocks hardly ever works
问题描述
我是一个前端 Magento 开发者,已经构建了很多我自己的主题,我想更好地了解 Magento 的 XML 块定位...
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...
我通常使用一个 local.xml
文件来操作一切,我可以如下定义一个块:
I normally use a local.xml
file to manipulate everything, I can define a block as follows:
<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>
这将在主页上创建一个块 (cms_index_index
),并且由于该块是在 root
下的一级创建的,我通常会通过添加以下内容来调用该块:
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:
<?php echo $this->getChildHtml('exampleBlock') ?>
...to 1column.phtml
(或 2columns-left
/right.phtml
, 3columns.phtml
> 等).通过将 cms_index_index
替换为适当的页面标记,可以将该块放置在任何页面上.
...to 1column.phtml
(or 2columns-left
/right.phtml
, 3columns.phtml
etc). The block can be placed on any page by substituting cms_index_index
for the appropriate page tag.
我在整个核心 XML 文件和教程中看到类似以下内容:
I see stuff like the following throughout the core XML files, and in tutorials:
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
content
是一个块,它是 magento 的一般页面结构的一部分,据我所知,before="content"
应该把它放在你期望的地方,无需使用 getChildHtml('exampleBlock')
,到目前为止还不错……但是,之前/之后似乎对我来说几乎没有用,而且我经常发现自己求助于 getChildHtml 方法作为备份,这并不总是理想的,并且意味着编辑比必要更多的 .phtml 文件.
content
is 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.
我试过了:
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
什么都没有出现...
<reference name="root">
<block type="core/template" name="example_block" after="header" template="page/html/example-block.phtml"/>
</reference>
仍然什么都没有......我也知道使用 before="-"
或 after="-"
在它的父块中的所有东西之前放置一些东西.我偶尔会有一些运气,但通常只是感到困惑和沮丧.
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.
我在到处搜索magento xml before/after not working"并开始怀疑是不是只有我会发生这种情况...谁能解释一下我什么时候可以使用之前/之后不可以使用位置块?上面的例子有什么问题?
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 1.7.0.2(发布时可用的最新版本)
I'm in magento 1.7.0.2 (latest available at time of posting)
这样做的主要动机是减少我需要编辑的核心 .phtml 文件的数量,以便添加 getChildHtml()
,所以如果有另一种 (XML) 方法来解决这个问题我很想知道...
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...
推荐答案
before
和 after
属性仅适用于以下两种情况之一:
The before
and after
attributes only work in one of two cases:
- 当你插入一个
core/text_list
块时 - 当你的模板块不带任何参数调用
getChildHtml
时
当你说
<reference name="root">
<block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/>
</reference>
你在告诉 Magento
you're telling Magento
嘿 Magento,将 example_block
放在 root
块中.
Hey Magento, put the
example_block
inside theroot
block.
当您将多个不同的块放入父级时,这些块具有隐式顺序.对于模板块,此顺序无关紧要,因为这些块是显式呈现的.
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() ?>
来自模板,然后 Magento 将按顺序呈现所有子块.
from a template, then Magento will render all the child blocks, in order.
其次,有一种特殊类型的块,称为文本列表"(core/text_list
/Mage_Core_Block_Text_List
).这些块再次按顺序自动渲染所有子项.content
块就是一个例子
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 content
block is an example of this
<block type="core/text_list" name="content"/>
这就是为什么您可以将块插入 content
并且它们会自动呈现.
That's why you can insert blocks into content
and they render automatically.
因此,在上面的示例中,您将块插入到 root
块中.root
块是一个模板块,其 phtml 模板使用带有显式参数的 getChildHtml
调用.因此 before
和 after
属性不会做你(和许多其他人,包括我)希望他们做的事情.
So, in your example above, you're inserting blocks into the root
block. The root
block is a template block whose phtml template uses getChildHtml
calls with explicit parameters. Therefore the before
and after
attributes don't do what you (and many others, including me) wish they did.
这篇关于Magento XML 使用 before/after 放置块几乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!