Magento XML使用before/after放置块几乎没有工作
我是一个前端Magento开发,已经构建了相当多的我自己的主题,我想了解Magento的XML块定位更好…
我通常使用一个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> 这将在主页(cms_index_index)上创建一个块,并且由于该块在根下创建一级,我通常通过添加以下命令来调用该块: <?php echo $this->getChildHtml('exampleBlock') ?> …到1column.phtml(或2columns-left / right.phtml,3columns.phtml等)。通过将cms_index_index替换为适当的页面标记,可以将该块放置在任何页面上。 我在核心XML文件和教程中看到类似以下内容: <reference name="root"> <block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/> </reference> 内容是一个块,它是magento的一般页面结构的一部分,从我的理解,之前=“内容”应该放在你期望的地方,而不需要使用getChildHtml(‘exampleBlock’),到目前为止好。然而,之前/之后几乎没有似乎为我工作,我经常发现自己诉诸getChildHtml方法作为备份,这并不总是理想的,意味着编辑更多的.phtml文件比必要。 我试过了: <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 =“ – ”在它的父块中的一切之前放置东西。我偶尔也有一些运气,但一般只是困惑和沮丧。 我已经在所有的地方“magento xml之前/之后不工作”的地方,开始怀疑是否只是我发生这种情况…任何人都可以解释,当我可以,不能使用之前/之后的位置块?上面的例子有什么问题? 我在magento 1.7.0.2(最新的发布时间) 这个的主要动机是减少我需要编辑的核心.phtml文件的数量只是为了添加一个getChildHtml(),所以如果有另一个(XML)的方式来解决这个我有兴趣知道…
before和after属性仅在以下两种情况下工作:
>当你插入一个core / text_list块 当你说 <reference name="root"> <block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/> </reference> 你告诉Magento
当你把一些不同的块放在一个父类中,这些块有一个隐含的顺序。对于模板块,此顺序无关紧要,因为这些块正在被显式渲染。 <?php echo $this->getChildHtml('example_block') ?> 但是,有两种情况下,顺序很重要。首先,如果你打电话 <?php echo $this->getChildHtml() ?> 从模板,然后Magento将渲染所有的子块,按顺序。 其次,有一种特殊类型的块称为“文本列表”(core / text_list / Mage_Core_Block_Text_List)。这些块会自动再次渲染所有子级。内容块是其示例 <block type="core/text_list" name="content"/> 这就是为什么你可以插入块到内容,并自动呈现。 所以,在上面的例子中,你将块插入根块。根块是一个模板块,其phtml模板使用带有显式参数的getChildHtml调用。因此,前后属性不会做你(和许多其他人,包括我)希望他们做的。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |