Magento结构块,内容块和phtml模板
我刚刚开始阅读关于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>
Magento中主要有两种类型的块
结构块: -这些块实际上定义了页块的结构。 这是内容块所在的位置
  示例: Header , Left , Right , Footer和Main块(在page.xml中定义) 
内容块: -这些块实际上持有内容。 取决于块的类型,由这些块保留的内容各不相同
  示例: - 任何自定义块, core/template cms/page板块, 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');
    }
    ......
}
  它是。  我们的结构块实际上通过后端分配了一个模板。  这意味着与我们的头文件块对应的模板驻留在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>
.....
  正如您在上面看到的那样,它使用getChildHtml()方法调用page.xml布局文件中定义的子块(换句话说,内容块)。 
  这意味着,如果您在header结构块内添加自定义子块并且未使用getChildHtml()方法在header.phtml调用它,则您的内容块不会显示在前端中。 
  您还可以设置header.phtml到块header通过page.xml这样 
<block type="page/html_header" name="header" as="header" template="page/html/header.phtml" />
这没有问题。 总而言之,我们通常不会说定义phtml文件的块是内容块还是结构块。 这两个块如何阻止纯粹取决于这些块的含义。
短注:一个有效的块可以包含其他内容块。 我们所做的大部分时间都是这样。 意味着将我们的自定义内容块添加到已经存在于magento中的另一个内容块。
  2)如果你在magento中查看不同的块,你可以看到,无论是结构块/内容块,块都可以通过布局或通过后端设置模板。  我们也可以使用观察者将模板设置为块。  我已经说过如何用模板header.phtml设置header结构块。  在这种情况下,它是通过后端。 
希望能帮助你理解这个概念。
编辑
不,你是绝对错误的。 在magento布局中包含页面的整个结构。 它包含需要为特定页面呈现的所有块。
  假设你有一个网址www.mydomain.com/index.php/customer/account 。  magento现在所做的是分割url并找到哪个模块生成这样的url。  所以上面的url就像这样分开 
base url => www.mydomain.com/index.php/
frontend name => customer/
action controller name => account/
method => index/
  现在,magento将查询哪个模块负责前端名称customer 。  它由Mage_Customer核心模块定义。  现在,magento寻找哪个控制器来处理这个url。  在我们的网址中,提到这一点的部分是account 。  所以它会在Mage_Customer模块中寻找AccountController.php文件。  现在又一次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();
}
  这是重要的部分。  首先该方法调用一个函数loadLayout() 。  这个简单的代码在curton背后做了很多工作。  它会生成一个大的布局树,与前台显示的页面相对应。  这个布局在哪里定义?  当然..这是在布局XML文件。  布局xml文件定义了应该为上面的url呈现哪些块,哪些不是。  例如在这种情况下,follwing句柄将由magento处理。 
default
STORE_default
THEME_frontend_default_default
customer_account_index
customer_logged_in
customer_account
  Magento会生成一个巨大的布局树,它将包含这些布局控件下指定的所有块。  布局句柄可以位于目录app/design/frontend/<your_package>/<your_theme>/layout下的任何布局XML文件中。  创建此布局树后,magento将使用代码renderLayout()渲染这些布局树。  基本上它会做的是将这些布局转换为html并呈现它(为此,它使用模板文件和皮肤)。 
现在你会怎么想? 布局XML很简单吗? :)
链接地址: http://www.djcxy.com/p/59489.html上一篇: Magento structural blocks, content blocks and phtml templates
