简体   繁体   English

使用 before/after 放置块的 Magento XML 几乎不起作用

[英]Magento XML using before/after to place blocks hardly ever works

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.xml file 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.phtml etc). ...到1column.phtml (或2columns-left / right.phtml3columns.phtml等)。 The block can be placed on any page by substituting cms_index_index for the appropriate page tag.通过将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>

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. content是一个块,它是 magento 的一般页面结构的一部分,据我所知, before="content"应该将它放在您期望的位置,而无需使用getChildHtml('exampleBlock') ,到目前为止一切顺利.. . 然而, before/after 似乎对我来说几乎不起作用,我经常发现自己求助于 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.仍然什么都没有......我也知道使用before="-"after="-"在它的父块中的所有东西之前放置一些东西。 I occasionally have some luck with that, but generally just get confused and frustrated.我偶尔会有一些运气,但通常只是感到困惑和沮丧。

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?我在到处搜索“magento xml 之前/之后不工作”并开始怀疑它是否只是我这种情况发生......谁能解释我何时可以和不能使用之前/之后定位块? What's wrong with the above examples?上面的例子有什么问题?

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)方法来解决这个问题,我很想知道......

The before and after attributes only work in one of two cases: beforeafter属性仅适用于以下两种情况之一:

  1. When you insert into a core/text_list block当您插入core/text_list块时
  2. When your template block calls getChildHtml without any parameters当您的getChildHtml不带任何参数调用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_block inside the root block.嘿 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 all the 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 ).其次,有一种特殊类型的块称为“文本列表”( 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 content块就是一个例子

<block type="core/text_list" name="content"/>

That's why you can insert blocks into content and they render automatically.这就是为什么您可以将块插入content并让它们自动呈现的原因。

So, in your example above, you're inserting blocks into the root block.因此,在上面的示例中,您将块插入到root块中。 The root block is a template block whose phtml template uses getChildHtml calls with explicit parameters. root块是一个模板块,其 phtml 模板使用带有显式参数的getChildHtml调用。 Therefore the before and after attributes don't do what you (and many others, including me) wish they did.因此beforeafter属性不会做你(和许多其他人,包括我)希望他们做的事情。

I wonder but it seems that Mage_Core_Block_Abstract :我想知道但似乎Mage_Core_Block_Abstract

public function getChildHtml($name = '', $useCache = true, $sorted = false)

doesn't render the blocks in order because of $sorted = false .由于$sorted = false ,不会按顺序呈现块。

So finally the order/sorting of blocks is by default only considered in Core/Block/Text/List - Block.所以最后块的顺序/排序默认只在Core/Block/Text/List - Block 中考虑。

If you want to ensure that child blocks were output in correct order you have to use:如果要确保以正确的顺序输出子块,则必须使用:

<?php echo $this->getChildHtml('', true, true) ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM