六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

Magento使用正规的方式输出网页(使用phtml文件输出内容)

[复制链接]

升级  95%

147

主题

147

主题

147

主题

举人

Rank: 3Rank: 3

积分
485
 楼主| 发表于 2013-2-4 23:45:27 | 显示全部楼层 |阅读模式
如果你已经学会了扩展模块的基本输出方法,那么本文一定是你需要的——使用正规的方法输出网页。

假设模块为Cartz_Hotel,我们想当访问
http://localhost/magento/index.php/hotel/my/room能够输出Hello , phtml Page
I. 建立controllers/MyController.php内容如下:

    <?php    class Cartz_Hotel_MyController extends Mage_Core_Controller_Front_Action{        public function roomAction(){          $this->loadLayout();          $this->renderLayout();       }    }
II. etc/config.xml
    <?xml version="1.0"?>    <config>       <modules>          <Cartz_Hotel>             <version>0.1.0</version>          </Cartz_Hotel>       </modules>       <frontend>          <routers>             <hotel>                <use>standard</use>                <args>                   <module>Cartz_Hotel</module>                   <frontName>hotel</frontName>                </args>             </hotel>          </routers>            <layout>                <updates>                    <hotel>                        <file>hotel.xml</file>                    </hotel>                </updates>            </layout>       </frontend>       <global>          <blocks>             <hotel><class>Cartz_Hotel_Block</class></hotel>          </blocks>       </global>    </config>


config.xml最重要的部分是
1. hotel.xml将稍后用来作为配置模块layout的文件
2. <class>Cartz_Hotel_Block</class>用来说明Block类命名规则(文件的目录位置)。

III. Block/Room.php

    <?php    class Cartz_Hotel_Block_Room extends Mage_Core_Block_Template{       public function getHello() {          return "Hello, phtml page";       }    }


IV. 建立phtml文件
$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<theme package>/<theme name>/template/hotel/room.phtml

    <h1><?php echo $this->getHello(); ?></h1>


V. 建立Layout文件
$MAGENTO_INSTALLED_DIR/app/design/<frontend>/<theme package>/<theme name>/layout/hotel.xml
    <?xml version="1.0"?>    <layout version="0.1.0">       <hotel_my_room>            <reference name="root">                <action method="setTemplate"><template>page/1column.phtml</template></action>            </reference>            <reference name="content">                <block type="hotel/room" name="hotel_room" template="hotel/room.phtml"/>            </reference>        </hotel_my_room>    </layout>


当访问http://localhost/magento/index.php/hotel/my/room时,Magento自动会定位到标签<hotel_my_room>对应的block。看
<block type="hotel/room" name="hotel_room" template="hotel/room.phtml"/>
block的type是hotel/room,将执行对应Cartz_Hotel_Block_Room类,然后找到对应的template/hotel/room.phtml文件。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表