标题块和topSearch(catelogsearch/form.mini.phtml)之间的Magento主题链接


Magento theme link between the header block and the topSearch (catelogsearch/form.mini.phtml)

我开始学习如何创建Magento主题。我开始了解什么是块以及在哪里可以找到特定的 phtml 文件,方法是将模板路径提示和将块名称添加到提示设置为是。下面是布局/页面.xml文件中的标题块。

<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>
            <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>

使用上面的XML块,它加载template/page/html/header.phtml文件。下面的代码位于 header.phtml 中。

 <?php echo $this->getChildHtml('topSearch') ?>

使用这个$this->getChildHtml('topSearch')方法,我希望它会在页面.xml文件的标题块中找到topSearch块并加载其phtml文件。但是页面内的标题块中没有 topSearch 块.xml。事实是,它从layout/catelogsearch.xml加载topSearch块。我的问题是它如何知道它需要从布局/分类搜索加载 topSearch 块.xml而不是?

Magento将在运行时将所有布局文件合并为一个,这一事实使这成为可能。因此,它实际上不会寻找topSearch参考,因为它不必这样做。合并完成后topSearch将成为page/html_header块的子块。

<default> <!--page handle, <default> is used on all pages-->
    <reference name="header"> <!--reference to page/html_header block-->
        <block type="core/template" name="top.search" as="topSearch" template="catalogsearch/form.mini.phtml"/>
    </reference>

推荐阅读:
布局
简介面向开发人员的Magento:第4部分 - Magento布局,块和模板