php 理解 Magento 中的 getChildHtml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19488885/
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
Understanding getChildHtml in Magento
提问by Anup_Tripathi
From the following line in 2columns-right.phtml
从 2columns-right.phtml 中的以下行
<div class="col-main">
<?php echo $this->getChildHtml('global_messages') ?>
<?php echo $this->getChildHtml('content') ?>
</div>
I am not able to understand that, from where the content of <?php echo $this->getChildHtml('content') ?>
is coming.
我无法理解,内容来自哪里<?php echo $this->getChildHtml('content') ?>
。
witch .phtml file is called to display the data by <?php echo $this->getChildHtml('content') ?>
调用女巫 .phtml 文件来显示数据 <?php echo $this->getChildHtml('content') ?>
回答by Darren Felton
If we're discussing the frontend of the website, the particular line you've asked about....
如果我们正在讨论网站的前端,那么您询问的特定行....
<?php echo $this->getChildHtml('content') ?>
is added to the Magento layout XML in app/design/frontend/base/default/layout/page.xml. In Magento version 1.8, you'll find it defined in lines 92-94.
被添加到 app/design/frontend/base/default/layout/page.xml 中的 Magento 布局 XML 中。在 Magento 1.8 版中,您会发现它在第 92-94 行中定义。
<block type="core/text_list" name="content" as="content" translate="label">
<label>Main Content Area</label>
</block>
By looking at the "type" attribute of this block tag, we can know what object class this section of the layout is. It comes from the "Core" module, and is of the block type Text List. The class name for this Mage_Core_Block_Text_List. (app/code/core/Mage/Core/Block/Text/List.php). Text Lists are simply block containers which purpose is to store additional child blocks inside them. You can add any number of child blocks to the text list and they will be rendered out either in the order they were added or the order they've been assigned.
通过查看这个block标签的“type”属性,我们可以知道这部分布局是什么对象类。它来自“核心”模块,属于块类型文本列表。此 Mage_Core_Block_Text_List 的类名。(app/code/core/Mage/Core/Block/Text/List.php)。文本列表只是块容器,其目的是在其中存储额外的子块。您可以将任意数量的子块添加到文本列表中,它们将按照添加顺序或分配顺序进行渲染。
So, to answer your question, there is no view script (.phtml file) that renders the contents of $this->getChildHtml('content').The blocks which have been added tothis block, may themselves have view scripts associated with them. To find out what view scripts those are, you'd have to find the layout XML which has added the block.
因此,为了回答您的问题,没有呈现 $this->getChildHtml('content') 内容的视图脚本(.phtml 文件)。已添加到此块的块本身可能具有与其关联的视图脚本。要找出那些是什么视图脚本,您必须找到添加了块的布局 XML。
For example, if I had the following layout file added to the frontend of my website's theme:
例如,如果我将以下布局文件添加到我网站主题的前端:
<?xml version="1.0"?>
<layout>
<default>
<reference name="content">
<block type="core/template" name="my_view_script" template="hello/world.phtml" />
</reference>
</default>
</layout>
The code above, would add the block with an object class of Mage_Core_Block_Template to the block with the name 'content' (which happens to be the one you asked about). Magento will then look for the view script in the following locations, in this order:
上面的代码会将具有 Mage_Core_Block_Template 对象类的块添加到名为“内容”的块中(这恰好是您询问的那个)。然后 Magento 将按以下顺序在以下位置查找视图脚本:
app/design/frontend/PACKAGE_NAME/THEME_NAME/template/hello/world.phtml
app/design/frontend/PACKAGE_NAME/default/template/hello/world.phtml
app/design/frontend/base/default/template/hello/world.phtml
First one that is found, is the one it will use. If no view script is found Magento will log an error in var/logs/system.log
(default log file setting) stating that the view script was not found. No output from the block will occur.
找到的第一个就是它将使用的那个。如果没有找到视图脚本,Magento 将在var/logs/system.log
(默认日志文件设置)中记录一个错误,指出没有找到视图脚本。不会发生块的输出。
Note that depending on your settings in System -> Configuration -> (General) Design, there may be additional package/theme
locations Magento will look in. There are also other scenarios such as if the "Custom Theme" is field is changed for individual CMS Pages, Catalog Categories, or Catalog Products, these individual model's view page may have an additional view script location (that will match the selected theme) that takes precedence over your site's default settings.
请注意,根据您在 System -> Configuration -> (General) Design 中的设置,package/theme
Magento可能会查看其他位置。还有其他情况,例如如果单个 CMS 页面的“自定义主题”字段更改,目录类别或目录产品,这些单独模型的视图页面可能具有优先于站点默认设置的附加视图脚本位置(将匹配所选主题)。
Magento will follow this same fallback logic when looking for translation files as well as layout XML files.
在查找翻译文件和布局 XML 文件时,Magento 将遵循相同的回退逻辑。
Please note, that it is perfectly acceptable to copy individual view scripts (avoid copying entire directories, copy over only view scripts you actually intend to modify) from app/design/frontend/base/default/template/
to your local theme, and customize them for the purposes of your website's theme. However, in order to have an upgrade compatible site, layout filesshould not be copied from base to your local theme. Doing so, does not follow upgrade compatible practices. Instead, XML Layout updates for your theme should be contained in app/design/frontend/PACKAGE_NAME/THEME_NAME/layout/local.xml
. There is no layout instructions from app/design/frontend/base/default/layout/*
, that cannot be removed/added-to/changed, what-have-you, with the proper XML instructions in local.xml.
请注意,将单个视图脚本(避免复制整个目录,仅复制您实际打算修改的视图脚本)从app/design/frontend/base/default/template/
本地主题复制并根据网站主题自定义它们是完全可以接受的。但是,为了拥有升级兼容的站点,不应将布局文件从基础复制到您的本地主题。这样做并不遵循升级兼容做法。相反,您的主题的 XML 布局更新应包含在app/design/frontend/PACKAGE_NAME/THEME_NAME/layout/local.xml
. 没有来自 的布局说明app/design/frontend/base/default/layout/*
,不能使用 local.xml 中的正确 XML 说明删除/添加/更改,随便你。