首页 文章

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

提问于
浏览
5

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

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

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

2)对于没有template =“XXX”的结构块,它最终如何与phtml文件链接?我的问题来自查看 Headers 块的上下文,如下所示,它的一些子块具有“模板”属性,但它们似乎都没有指向“\ 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>

1 回答

  • 10

    Magento主要有两种类型的块

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

    示例: HeaderLeftRightFooterMain blocks(在page.xml中定义)

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

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

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

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

    它就是 . 实际上,我们的结构块通过后端分配了一个模板 . 这意味着与我们的 Headers 块对应的模板位于 app/design/frontend/<your_package>/<your_theme>/template/page/html/header.phtml 中 . 如果打开该文件,您可以看到该模板实际上是使用html,css和js定义页面的 Headers 部分,并使用方法 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 设置为块 headerpage.xml

    <block type="page/html_header" name="header" as="header" template="page/html/header.phtml" />
    

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

    Short Note : 强大的块可以包含其他内容块 . 我们正在做的大部分时间都是这样 . 意味着将我们的自定义内容块添加到已存在于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/
    

    现在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呈现的块以及未呈现的块 . 对于此上下文中的示例,后续句柄将由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很简单吗? :)

相关问题