咯廊死计币抢蹈烩化虱兜稍谎
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<h3 style="background: lightskyblue">我是页面的页头header</h3> <p>我是index控制器index操作的模板文件</p> <h3 style="background: lightskyblue">我是页面的尾部footer</h3>
执行:
分离公共的头部与尾部文件
创建目录公共模板目录base
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\base\header.html:
<h3 style="background: lightskyblue">我是页面的页头header</h3>
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\base\footer.html:
<h3 style="background: lightskyblue">我是页面的尾部footer</h3>
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
{include file="base/header" /} <p>我是index控制器index操作的模板文件</p> {include file="base/footer" /}
执行:
配置系统文件:
<?php return [ 'template'=>[ 'layout_on'=>true, 'layout_name'=>'layout' ], ];
创建layout.html
其中:{__CONTENT__}是要被替换的内容
{include file="base/header" /} {__CONTENT__} {include file="base/footer" /}
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我是index控制器index操作的模板文件</p>
执行:
创建user():
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { return $this->fetch(); } public function user() { return $this->fetch(); } }
创建user模板文件:
<p>我是index控制器user操作的模板文件</p>
执行:
删除模板的布局配置:
<?php return [ // 'template'=>[ // 'layout_on'=>true, // 'layout_name'=>'layout' // ], ];
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
{layout name="layout"} <p>我是index控制器index操作的模板文件</p>
执行:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { $this->view->engine->layout(true);//true开启模板布局 return $this->fetch(); } public function user() { return $this->fetch(); } }
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\view\index\index.html:
<p>我是index控制器index操作的模板文件</p>
执行:
关闭模板布局
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { $this->view->engine->layout(false);//false关闭模板布局 return $this->fetch(); } public function user() { return $this->fetch(); } }
执行,没有生效:
关闭缓存:
删除图示模板缓存文件
再次执行:
自定义布局文件:
D:\phpStudy\PHPTutorial\WWW\tp5\application\index\controller\Index.php:
<?php namespace app\index\controller; class Index extends \think\Controller { public function index() { $this->view->engine->layout('mylayout');//自定义模板布局文件 return $this->fetch(); } public function user() { return $this->fetch(); } }
创建mylayout布局文件
{include file="base/header" /} {__CONTENT__} {include file="base/footer" /}
执行,执行前记得先删除缓存文件
峦玫惠浑送速戌苫碴村睦潭肌