1 Test>News>Block>Hello>Hello.php,创建一个类,写一个简单函数:
class Test_News_Block_Hello_Hello extends Mage_Core_Block_Template{
public function hello(){ return " hello world! hello magento "; }}
文件是php文件,首行的<?php 别掉了,class名称看出来了都是套路吧,路径该用下划线连接而已,至于继承的类暂时先不管。需要说明的是在这样的Block文件中,到具体的项目时,这里用来做一些事物处理,例如把处理的结果返回至phtml页面中显示在前台,这里是介绍而已,直接返回一个字符串。
2 去etc>config.xml中注册Block
<conifg>···<global><blocks><news><class>Test_News_Block</class><news><blocks><global> ···</config>
在xml文件中配置它以后,后面可以通过xml文件配置中使用news即可定位至Test_News_Block目录
3 app>design>frontend>base>layout,新建local.xml(如果您的模板和布局不是在这里需要去对应的模板路径下)
<layout version="0.1.0">
<news_index_index>
<block type="news/hello_hello" name="root" output="toHtml" template="news/hello.phtml" />
<news_index_index>
</layout>
根据前面的配置,news_index_index即是index控制器中的index函数,block中的news根据配置就到了Block目录,hello_hello到Hello.php文件中,template是指定布局文件
4 根据上一步到tempalte目录下创建news/hello.phtml
输入一句代码:<?php echo $this->hello(); ?>
根据上一步的配置,这里的$this是指Test/News/Block/Hello/Hello.php
5 到indexAction文件中注释掉之前的echo代码,重写两行 $this->loadLayout(); $this->rendenerLayout(); 用于加载和显示布局
6 刷新缓存,重新编译,因为涉及到.php文件,然后刷新之前的url,可以看见页面显示 hello world!hello magento