模板布局和模板继承
注:如果开启了全局模板配置:’layout_on’ => true,
则:模板文件解析顺序为:layout.html -> header.html/test2.html/footer.html
如果关闭了全局模板配置,则模板解析顺序为:test2.html -> base.html -> header.html/footer.html/block区域解析。
1.在模板配置文件config/template.php中,填写以下代码 :
// 开启全局模板布局'layout_on' => false,// 全局模板配置文件'layout_name' => 'layout',// 全局模板内容区定义,默认为'__CONTENT__'// 'layout_item' => '__TEXT__',
2.在view目录下新建public目录,下面新建base.html/header.html/footer.html文件。
<!--基础模板base--><!--模板头部-->{include file="public/header1" /}<!--区块-->{block name="body"}主体{/block}<!--模板尾部-->{include file="public/footer1" /}
3.在View目录新建全局模板配置文件layout.html
{include file='header' /}{__CONTENT__}{include file='footer' /}
4.在类对应的目录下,新建和方法同名的HTML文件,如test2.html
<!--继承基础模板-->{extend name="public/base" /}<!--body区块的实现-->{block name="body"}{__block__}<h2>我是模板继承的案例</h2>{/block}
<?phpnamespace app\index\controller;use think\Controller;class Demo8 extends Controller{public function test1(){return $this->view->fetch();}//模板继承public function test2(){return $this->view->fetch();}}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号