预备知识
在本文开始之前,确保之前你已经阅读了
I.第一个Magento扩展
问题提出
本文仍然以I提供的代码为基础, 然后进行增强。如若输出文字不仅仅是My First Module这么简单,如:
<dl id="narrow-by-list">
<dt class="odd">Category</dt>
<dd class="odd">
<ol>
<li><a href="furniture.html?cat=22">Living Room</a>(4)</li>
<li><a href="furniture.html?cat=23">Bedroom</a>(2)</li>
</ol>
</dd>
<dt class="even">By Price</dt>
<dd class="even">
<ol>
<li><a href="furniture.html?price=1%2C1000"><span class="price">$0.00</span> - <span class="price">$1,000.00</span></a>(5)</li>
<li><a href="furniture.html?price=3%2C1000"><span class="price">$2,000.00</span> - <span class="price">$3,000.00</span></a>(1)</li>
</ol>
</dd>
<dt class="last odd">By Color</dt>
<dd class="last odd">
<ol>
<li><a href="furniture.html?color=59">Brown</a>(2)</li>
<li><a href="furniture.html?color=26">Red</a>(4)</li>
</ol>
</dd>
</dl>
前面HTML代码在浏览器中显示效果如下:
显然,应付如上大块Html的输出,使用php的echo是令人诟病的,我们更愿意使用一个单独的php模板文件进行这些文本输出(其实大家一直这么做,不是么).
Magento的解决方案
Magento使用布局(Layout)、块(Block)来剥离网页的显示部分。
1. 块(Block)
一个Block是页面的一小块HTML, 一个页面由若干个Block组成。
一般情况下,Block的输出(页面一块的生成)是由一个功能php和
一个模板phtml(实际上就是php)共两个文件结合完成。
2. 布局(Layout)
前面说一个页面有若干的块(Block)组成,那么这些Block按照什么样的层级结构组织在一起,则需要一个Layout配置文件(其格式为xml),下面是一个Layout文件例子:
<?xml version="1.0"?>
<layout version="0.1.0">
<cms_page translate="label">
<label>CMS Pages (All)</label>
<reference name="content">
<block type="core/template" name="page_content_heading" template="cms/content_heading.phtml"/>
<block type="page/html_wrapper" name="cms.wrapper">
<action method="setElementClass"><value>std</value></action>
<block type="cms/page" name="cms_page"/>
</block>
</reference>
</cms_page>
</layout>
上面代码的含义是:当访问http://{your_server}/cms/page时,解析该xml块,该页面包含两个block,分别名字为page_content_heading和cms.wrapper,在cms_wrapper block中还包含一个子block名字为cms_page.
系列文章I的改造
1. 修改app/code/{NS}/{Module}/etc/controllers/MyContronller
<?php
class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{
public function helloAction() {
$this->loadLayout()->renderLayout();
}
}
2. 修改app/code/{NS}/{Module}/etc/config.xml
新增
<config>
.....
<frontend>
<!-- 指定layout文件的名字 -->
<layout>
<updates>
<hotel><file>hotel.xml</file></hotel>
</updates>
</layout>
</frontend>
<global>
<!-- block类的前缀指定 -->
<blocks>
<hotel><class>Cartz_Hotel_Block</class></hotel>
</blocks>
</global>
...
</config>
3. 新增app/code/{NS}/{Module}/Block/Blog.php
<?php
class Cartz_Hotel_Block_Blog extends Mage_Core_Block_Template {
// 稍后在phtml中调用
public function getTitle() {
return 'Shop By';
}
}
4. 新增app/design/frontend/default/default/layout/hotel.xml
<?xml version="1.0"?>
<layout version="0.1.0">
<hotel_my_hello>
<reference name="root">
<action method="setTemplate"><template>moneybookers/blank.phtml</template></action>
</reference>
<reference name="content">
<block type="hotel/blog" name="blog" template="blog.phtml" />
</reference>
</hotel_my_hello>
</layout>
4. 新增app/design/frontend/default/default/template/blog.phtml
<h1><?php echo $this->getTitle(); ?></h1>
<dl id="narrow-by-list">
<dt class="odd">Category</dt>
<dd class="odd">
<ol>
<li><a href="furniture.html?cat=22">Living Room</a>(4)</li>
<li><a href="furniture.html?cat=23">Bedroom</a>(2)</li>
</ol>
</dd>
<dt class="even">By Price</dt>
<dd class="even">
<ol>
<li><a href="furniture.html?price=1%2C1000"><span class="price">$0.00</span> - <span class="price">$1,000.00</span></a>(5)</li>
<li><a href="furniture.html?price=3%2C1000"><span class="price">$2,000.00</span> - <span class="price">$3,000.00</span></a>(1)</li>
</ol>
</dd>
<dt class="last odd">By Color</dt>
<dd class="last odd">
<ol>
<li><a href="furniture.html?color=59">Brown</a>(2)</li>
<li><a href="furniture.html?color=26">Red</a>(4)</li>
</ol>
</dd>
</dl>
5. 重新访问:http://www.emb.info/hotel/my/hello