Magento结构块、内容块和phtml模板
我刚刚开始阅读Magentos(1.9CE)布局以及它如何处理XML和PHTML文件。 我遇到了结构块和内容块 我正在查看Magento 1.9安装的RWD包默认主题的page.xml文件。 我在下面粘贴了我认为是page.xml文件的页眉、内容和页脚 我的问题 1) 在为块分配“template=“XXXX.phtml”属性时,是否将其视为内容块?如果不是,则称为结构块 2) 对于没有template=“XXX”的结构块,它最终是如何与phtml文件链接的?我的问题来自于查看标题块的上下文,如下所示,它的一些子块具有“template”属性,但它们似乎都没有指向“\template\page\html”中的header.phtml“目录,我一直在编辑它,以自定义Magento站点欢迎消息的外观Magento结构块、内容块和phtml模板,magento,layout,block,php,Magento,Layout,Block,Php,我刚刚开始阅读Magentos(1.9CE)布局以及它如何处理XML和PHTML文件。 我遇到了结构块和内容块 我正在查看Magento 1.9安装的RWD包默认主题的page.xml文件。 我在下面粘贴了我认为是page.xml文件的页眉、内容和页脚 我的问题 1) 在为块分配“template=“XXXX.phtml”属性时,是否将其视为内容块?如果不是,则称为结构块 2) 对于没有template=“XXX”的结构块,它最终是如何与phtml文件链接的?我的问题来自于查看标题块的上下文,如
<block type="page/html_header" name="header" as="header">
<block type="page/template_links" name="top.links" as="topLinks"/>
<block type="page/switch" name="store_language" as="store_language" template="page/switch/languages.phtml"/>
<block type="core/text_list" name="top.menu" as="topMenu" translate="label">
<label>Navigation Bar</label>
<block type="page/html_topmenu" name="catalog.topnav" template="page/html/topmenu.phtml">
<block type="page/html_topmenu_renderer" name="catalog.topnav.renderer" template="page/html/topmenu/renderer.phtml"/>
</block>
</block>
<block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
<label>Page Header</label>
<action method="setElementClass"><value>top-container</value></action>
</block>
<block type="page/html_welcome" name="welcome" as="welcome"/>
</block>
<block type="core/text_list" name="content" as="content" translate="label">
<label>Main Content Area</label>
</block>
<block type="page/html_footer" name="footer" as="footer" template="page/html/footer.phtml">
<block type="page/html_wrapper" name="bottom.container" as="bottomContainer" translate="label">
<label>Page Footer</label>
<action method="setElementClass"><value>bottom-container</value></action>
</block>
<block type="page/switch" name="store_switcher" as="store_switcher" after="*" template="page/switch/stores.phtml"/>
<block type="page/template_links" name="footer_links" as="footer_links" template="page/template/links.phtml">
<action method="setTitle"><title>Quick Links</title></action>
</block>
<block type="page/template_links" name="footer_links2" as="footer_links2" template="page/template/links.phtml">
<action method="setTitle"><title>Account</title></action>
</block>
<!-- This static block can be created and populated in admin. The footer_links cms block can be used as a starting point. -->
<!--<block type="cms/block" name="footer_social_links">
<action method="setBlockId"><block_id>footer_social_links</block_id></action>
</block>-->
</block>
导航栏
页眉
顶部容器
主要内容区
页脚
底部容器
快速链接
账户
Magento中主要有两种类型的块
页眉
,左
,右
,页脚
和主
块(在page.xml中定义)core/template
block、cms/page
block等cms/page
块用于保存我们通过管理部分设置的cms页面内容<代码>目录/产品视图块用于保存产品视图内容。正如您已经注意到的,这两个内容块用于保存内容,但内容因块而异,具体取决于指定的类型。说到这里,让我们来看看你的问题
1) 结构块保存页面的结构。内容块位于每个结构块之下。因此,在上面的布局代码中,类型为page/html\u header
的块是一个结构块。而该块中的所有其他块都是上述结构块的内容块。换句话说,它们是标题结构块的子项。现在让我们看看背面的标题块
#File : app/code/core/Mage/Page/Block/Html/Header.php
<?php
class Mage_Page_Block_Html_Header extends Mage_Core_Block_Template
{
public function _construct()
{
$this->setTemplate('page/html/header.phtml');
}
......
}
如上所述,它使用getChildHtml()
方法调用在page.xml
布局文件中定义的子块(换句话说就是内容块)
这意味着,如果您在header
结构块中添加自定义子块,并且没有使用getChildHtml()
方法在header.phtml
中调用它,那么您的内容块将不会显示在前端
您还可以通过page.xml
将header.phtml
设置为块header
,如下所示
<block type="page/html_header" name="header" as="header" template="page/html/header.phtml" />
base url => www.mydomain.com/index.php/
frontend name => customer/
action controller name => account/
method => index/
现在,magento将查询负责前端名称客户的模块。它由Mage_Customer
核心模块定义。现在,magento查找处理此url的控制器。在我们的url中,提到这一点的部分是帐户
。因此,它将在Mage_Customer
模块中查找AccountController.php
文件。现在,magento再次查找处理url的whcih方法。但是在我们的url中没有指定方法。因此,它将假定方法索引。现在看看这个方法
#File:app/code/core/Mage/Customer/controllers/AccountController.php
/**
* Default customer account page
*/
public function indexAction()
{
$this->loadLayout();
// some other codes
$this->renderLayout();
}
这是重要的部分。首先,该方法调用函数loadLayout()
。这个简单的代码在curton后面做了很多工作。它将生成一个大的布局树,对应于将在前端显示的页面。该布局定义在哪里?当然它位于布局XML文件中。布局xml文件定义了应该为上述url呈现的块以及不应该呈现的块。例如,在此上下文中,以下句柄将由magento处理
default
STORE_default
THEME_frontend_default_default
customer_account_index
customer_logged_in
customer_account
Magento将生成一个巨大的布局树,其中包含在这些布局句柄下指定的所有块。布局句柄可以位于app/design/frontend///Layout
目录下的任何布局XML文件中。创建此布局树后,magento将使用代码renderLayout()
渲染这些布局树。基本上,它将把这些布局转换成html并呈现出来(为此它使用模板文件和皮肤)
现在你会怎么想?布局XML有那么简单吗?:) 嗨,程序员。谢谢你的透彻解释,这对我来说非常有意义。请告诉我我的理解是否正确:我习惯了MVC,现在我知道XML布局文件与站点上的实际可见布局几乎没有任何关系,而更多的是与确定要使用的“可用”内容有关。可用和最终使用的内容取决于PHP代码在phtml文件中实际调用的内容。与“哪个phtml”文件的关系可以通过后端或模板属性中的XML文件来完成。我说得通吗?不。在magento中,整个布局由一个布局文件控制。没有布局文件,您无法看到前端和后端的任何内容。实际上,根据url中的每个请求,magento生成相应的布局树,然后将其转换为html,然后呈现。请看我的更新。这确实让事情变得更加清晰
default
STORE_default
THEME_frontend_default_default
customer_account_index
customer_logged_in
customer_account