magento - Magento结构 block ,内容 block 和phtml模板

标签 magento layout block php

我刚刚开始阅读magentos(1.9ce)布局,以及它如何处理xml和phtml文件。
我遇到了结构块和内容块。
我正在查看magento 1.9 installed rwd package default theme的page.xml文件。
我在下面粘贴了page.xml文件的页眉、内容和页脚。
我的问题
1)当一个块被赋予“template=”xxxx.phtml“属性时,它是否被认为是一个内容块?如果不是,它被称为结构块?
2)对于没有template=“xxx”的结构块,它最终如何与phtml文件链接?我的问题来自如下所示的header块的上下文,它的一些子块具有“template”属性,但它们似乎都没有指向“\template\page\html”目录中的header.phtml,我一直在编辑该目录以自定义magento站点welcome mes的外观鼠尾草。

<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中主要有两种类型的块
结构块:这些块实际上定义了页面块的结构。这是内容块所在的位置
示例:HeaderLeftRightFooterMain块(在page.xml中定义)
内容块:-这些块实际上包含内容。根据块的类型,这些块保存的内容会有所不同
示例:-任何自定义块,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');
    }

    ......
}

就在这里。我们的结构块实际上通过后端分配了一个模板。这意味着与头块相对应的模板位于app/design/frontend/<your_package>/<your_theme>/template/page/html/header.phtml中。如果打开该文件,可以看到模板实际上是使用html、css和js定义页面的头部分,并使用方法调用其子块。上述文件的某些部分如下所示。
.....
<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布局文件中定义的子块(换句话说,内容块)。
这意味着,如果您在getChildHtml()结构块中添加自定义子块,并且没有使用header方法在header.phtml中调用它,那么您的内容块将不会显示在前端。
您还可以将getChildHtml()设置为块header.phtmlheader中,如下所示
<block type="page/html_header" name="header" as="header" template="page/html/header.phtml" />

这没有问题。所以简而言之,我们通常不能说定义phtml文件的块是内容块或结构块。这两个块的差异完全取决于这些块所包含的内容。
简要说明:内容块可以包含其他内容块。我们现在做的大部分时间就是这样。意味着将自定义内容块添加到magento中已存在的另一个内容块。
2)如果在Magento中查看不同的块,可以看到,无论结构块/内容块如何,块都可以通过布局或通过后端使用模板设置。我们还可以使用观察者将模板设置为块。我已经说明了如何使用模板设置结构块。
. 在这种情况下,它是通过后端。
希望能帮助你理解这个概念。
编辑
不,你完全错了。在magento布局中保存页面的整个结构。它保存了需要为特定页面呈现的所有块。
假设您有一个urlpage.xml。magento现在所做的是分割url并找出哪个模块生成这样的url。所以上面的url像这样拆分
base url => www.mydomain.com/index.php/
frontend name => customer/
action controller name => account/
method => index/

现在,magento将查询负责前端名称header的模块。它由核心模块定义。现在,magento查找处理此url的控制器。在我们的url中,提到这一点的部分是header.phtml。因此它将在www.mydomain.com/index.php/customer/account模块中查找customer文件。现在,magento再次查找处理url的whcih方法。但是在我们的url中没有指定方法。因此,它将假定方法Mage_Customer因此。现在看看这个方法。
#File:app/code/core/Mage/Customer/controllers/AccountController.php
/**
 * Default customer account page
 */
public function indexAction()
{
    $this->loadLayout();

    // some other codes

    $this->renderLayout();
}

这是重要的部分。首先,该方法调用一个函数account。这个简单的代码在柯顿背后做了很多工作。它将生成一个大的布局树,与将要在前端显示的页面相对应。这些布局是在哪里定义的?当然。。它在布局XML文件中。布局XML文件定义了应该为上述URL呈现的块和不应该呈现的块。例如,下面的句柄将由magento处理。
default
STORE_default
THEME_frontend_default_default
customer_account_index
customer_logged_in
customer_account

magento将生成一个巨大的布局树,它将包含在这些布局句柄下指定的所有块。布局句柄可以出现在AccountController.php目录下的任何布局xml文件中。创建此布局树后,magento将使用代码Mage_Customer呈现这些布局树。基本上,它要做的是将这些布局转换成html并呈现出来(为此它使用模板文件和皮肤)。
现在你会怎么想?布局xmls很简单吗?:)

关于magento - Magento结构 block ,内容 block 和phtml模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134065/

相关文章:

magento - 检查 Magento 购物车中是否有具有特定 AttributeSetName 的产品

magento - 在magento中调用我的模块中另一个模块的助手

magento-1.5 - Magento:如何翻译 Action 标签内容?

c - 是否 (int i = 0; i < n; i++;) 用作 block 而不是 {int i = 0;我<n; i++;} 在 C 中有任何意义吗?

hadoop - 运行sqoop导出时在hdfs中需要其他 block

css - 在 Squarespace 上交换移动 View 的文本 block 顺序

magento - 如何在magento中获取最后运行的事务ID

html - 垂直布局(标题 - 菜单 - 内容 - 底部)

html - 媒体查询和 float /中断跨度

c++ - 如何以编程方式更改布局中小部件的顺序?