Magento结构块,内容块和phtml模板

aDv*_*Dvo 5 php layout block magento

我刚刚开始阅读Magentos(1.9 CE)布局以及它如何与XML和PHTML文件一起工作.我遇到了结构块和内容块.

我正在查看Magento 1.9安装的RWD包DEFAULT主题的page.xml文件.我已经粘贴在我认为是page.xml文件中的标题,内容和页脚的下方.

我的问题1)当一个块被分配了"template ="XXXX.phtml"属性时,它被认为是一个内容块吗?如果不是,它被称为结构块?

2)对于没有template ="XXX"的结构块,它最终如何与phtml文件链接?我的问题来自查看标题块的上下文,如下所示,它的一些子块具有"模板"属性,但它们似乎都没有指向"\ template\page\html"目录中的header.phtml,我一直在编辑,以自定义Magento网站欢迎信息的外观.

<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>
Run Code Online (Sandbox Code Playgroud)

Raj*_*omy 10

Magento主要有两种类型的块

  1. 结构块: -这些块实际上定义了页面块的结构.这是内容块所在的位置

    例如:Header,Left,Right,FooterMain块(在page.xml定义)

  2. 内容块: -这些块实际上是持有内容.取决于块的类型,这些块保持的内容会有所不同

    示例: - 任何自定义块,core/template块,cms/page块等.

通常,每个内容块都应该位于上述任何结构块之下.这些内容块根据其类型保持不同的内容.例如,一个cms/page块打算保存我们通过管理部分设置的cms页面内容.catalog/product_view块用于保存产品视图内容.正如您已经注意到的那样,这两个内容块用于保存内容,但内容区别于块之间,具体取决于指定的类型.有了这个说,让我们看看你的问题

1)结构块保持页面的结构.内容块位于每个结构块下.因此在上面的布局代码中,具有类型的块page/html_header是结构块.虽然此块内的所有其他块都是上述结构块的内容块.换句话说,它们是标题结构块的子代.现在让我们看一下这个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');
    }

    ......
}
Run Code Online (Sandbox Code Playgroud)

它就是.实际上,我们的结构块通过后端分配了一个模板.这意味着与我们的标题块对应的模板位于app/design/frontend/<your_package>/<your_theme>/template/page/html/header.phtml.如果打开该文件,您可以看到该模板实际上是使用html,css和js定义页面的标题部分,并使用方法调用其子块getChildHtml().上述文件的某些部分如下所示.

.....
<div class="quick-access">
            <?php echo $this->getChildHtml('topSearch') ?>
            <p class="welcome-msg"><?php echo $this->getChildHtml('welcome') ?> <?php echo $this->getAdditionalHtml() ?></p>
            <?php echo $this->getChildHtml('topLinks') ?>
            <?php echo $this->getChildHtml('store_language') ?>

            .....
</div>

.....
Run Code Online (Sandbox Code Playgroud)

如您所见,它使用方法调用page.xml布局文件中定义的子块(换句话说内容块)getChildHtml().

这意味着,如果在header结构块内添加自定义子块并且未在header.phtmlusing getChildHtml()方法中调用它,则您的内容块将不会显示在前端中.

您还可以设置header.phtml该块header通过page.xml这样的

<block type="page/html_header" name="header" as="header" template="page/html/header.phtml" />
Run Code Online (Sandbox Code Playgroud)

这没有问题.简而言之,我们通常不能说定义phtml文件的块是内容块或strutural块.如何阻止这些阻止纯粹取决于这些阻止的阻碍.

简短注意:强大的块可以包含其他内容块.我们正在做的大部分时间都是这样.意味着将我们的自定义内容块添加到已存在于magento中的另一个内容块.

2)如果查看magento中的不同块,您可以看到,无论结构块/内容块如何,块都可以通过布局或后端使用模板进行设置.我们也可以使用观察者将模板设置为块.我已经说明了如何header使用模板设置结构块header.phtml .在这种情况下,它是通过后端端.

希望能帮助您理解这个概念.

编辑

不,你是绝对错误的.在magento布局中保存页面的整个结构.它包含需要为特定页面呈现的所有块.

假设你有一个网址www.mydomain.com/index.php/customer/account.magento现在做的是它会拆分网址并找到哪个模块生成这样的网址.所以上面的url像这样分开了

base url => www.mydomain.com/index.php/
frontend name => customer/
action controller name => account/
method => index/
Run Code Online (Sandbox Code Playgroud)

现在,magento将查询负责前端名称的模块customer.它由Mage_Customer核心模块定义.现在,magento会查找处理此URL的控制器.在我们的网址中,提到这一点的部分是account.所以它会AccountController.phpMage_Customer模块中查找文件.现在再次magento寻找处理url的whcih方法.但在我们的网址中没有指定方法.所以它将采用这种方法index.现在看看那个方法.

#File:app/code/core/Mage/Customer/controllers/AccountController.php
/**
 * Default customer account page
 */
public function indexAction()
{
    $this->loadLayout();

    // some other codes

    $this->renderLayout();
}
Run Code Online (Sandbox Code Playgroud)

这是重要的部分.首先,该方法调用一个函数loadLayout().这个简单的代码在curton背后做了很多工作.它将生成一个大的布局树,该树对应于将在前端显示的页面.这个布局在哪里定义?当然..它是布局XML文件.布局xml文件定义了应该为此上面的URL呈现的块以及未呈现的块.对于此上下文中的示例,后续句柄将由magento处理.

default
STORE_default
THEME_frontend_default_default
customer_account_index
customer_logged_in
customer_account
Run Code Online (Sandbox Code Playgroud)

Magento将生成一个巨大的布局树,它将包含在这些布局句柄下指定的所有块.布局句柄可以包含在目录下的任何布局XML文件中app/design/frontend/<your_package>/<your_theme>/layout.创建此布局树后,magento将使用代码呈现这些布局树renderLayout().基本上它将做的是将这些布局转换为html并渲染它(为此它使用模板文件和皮肤).

那你觉得怎么样?布局XML很简单吗?:)