7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置

咯廊死计币抢蹈烩化虱兜稍谎

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第1张

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第2张

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>

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第3张


分离公共的头部与尾部文件

创建目录公共模板目录base

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第4张

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" /}

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第5张


7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第6张

配置系统文件:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第7张

<?php
return [
    'template'=>[
        'layout_on'=>true,
        'layout_name'=>'layout'
    ],
];

创建layout.html

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第8张

其中:{__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>

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第9张


创建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模板文件:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第10张

<p>我是index控制器user操作的模板文件</p>

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第11张


7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第12张

删除模板的布局配置:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第13张

<?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>

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第14张


7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第15张

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>

执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第16张

关闭模板布局

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();
    }
}

执行,没有生效:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第17张

关闭缓存:

删除图示模板缓存文件

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第18张

再次执行:

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第19张


自定义布局文件:

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布局文件

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第20张

{include file="base/header" /}
{__CONTENT__}
{include file="base/footer" /}

执行,执行前记得先删除缓存文件

7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第21张


7-12 ThinkPHP5模板布局:全局配置/模板配置/控制器配置第22张


峦玫惠浑送速戌苫碴村睦潭肌