thinkphp 实现模板在同一个目录下
thinkphp 默认模板目录结构是
|-Tpl
|----模块名
|--------操作名.html
|--------操作名.html
|----模块名
|--------操作名.html
|--------操作名.html
现在如果想实现这样的结构:
|-Tpl
|----模块名_操作名.html|----模块名_操作名.html|----模块名_操作名.html|----模块名_操作名.html
笨方法是这么写的
$this->display('./Tpl/模块名_操作名.html');如果碰上项目分组,还得加上项目路径,实在是麻烦。
其实有一种办法,不需要自己写路径即可实现。
示例:
我们先创建一个公共的模块CommonAction.class.php 代码如下:
if(!defined("ACCESS"))exit('Access Denied!');//猜一猜有什么用
class CommonAction extends Action {
//自定义一个_display()方法
function _display(){
parent::display(APP_PATH.'Tpl/'.MODULE_NAME.'_'.ACTION_NAME.'.html');
}
}
然后,其他模块都继承这个CommonAction
define("ACCESS",TRUE);//猜猜有什么作用
class IndexAction extends CommonAction {
function index(){
$this->_display();
}
}
在项目下的Tpl目录中新建 Index_index.html ,然后访问首页,得到满意的结果!