magento模板中XML与phtml关系(三)

2011-07-29 14:36:00

这次继续进行magento模板制作的探讨,上一次说了.xml可以做到.phtml做不到的事情。今天说说xml和phtml是怎么配合着来生成我们的magento模版的。

本次讲解,使用magento 1.4版本,使用base中的default模板。

例子是最快速了解magento的方式了,我们来看下magento头部的搜索框,是怎么显示出来的。

在:app\design\frontend\base\default\layout\catalogsearch.xml 中有声明定义
1234<default> <referencename="header"> <blocktype="core/template"name="top.search"as="topSearch"template="catalogsearch/form.mini.phtml"/> </reference>

然后在:app\design\frontend\base\default\template\page\html\header.phtml 中调用输出
12<div class="quick-access"> <?php echo$this->getChildHtml('topSearch') ?>

我们看到在XML中有代码片段 as=”topSearch”, 然后在phtml的 getChildHtml( )的参数就是topSearch,所以啊,这就是关键点。

我们要将这个搜索从模板的头部(header)移动到模板的底部(footer)怎么办呢?

那么就先将上面的XML中的<reference name=”header”> 改成<reference name=”footer”>,

然后将app\design\frontend\base\default\template\page\html\header.phtml中的<?php echo $this->getChildHtml(‘topSearch’) ?>删掉将其添加到:app\design\frontend\base\default\template\page\html\footer.phtml中的适当位置。

这个时候我们就发现
<reference name="header"> 对应的就是app\design\frontend\base\default\template\page\html\header.phtml文件<reference name="footer"> 对应的就是app\design\frontend\base\default\template\page\html\footer.phtml为甚么呢?我们看下:app\design\frontend\base\default\layout\page.xml文件中的定义这个footer和header的代码片段12345678910111213<pre class="brush:xml"><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"template="page/switch/stores.phtml"/> <block type="page/template_links"name="footer_links"as="footer_links"template="page/template/links.phtml"/></block></pre><p>看这第一行代码,就说的很清楚了,name="footer"并且template="page/html/footer.phtml"。所以这个<reference name="footer">就是在<br>对footer.phtm中的getChildHtml( )进行定义。<br>如果是初学者,读到这里还不懂,没关系。因为我开始看着也眼花,我们又不是专业人士,很正常。但是我们要善于去照猫画虎,去总结规律。<br>我们可能不懂,但是我们会用,这就是一个好的开始,多练。慢慢的就什么都懂了。

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注