Blogger Information
Blog 94
fans 0
comment 0
visits 92100
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模板布局和模板继承
可乐随笔
Original
1230 people have browsed it

模板布局和模板继承
注:如果开启了全局模板配置:’layout_on’ => true,
则:模板文件解析顺序为:layout.html -> header.html/test2.html/footer.html
如果关闭了全局模板配置,则模板解析顺序为:test2.html -> base.html -> header.html/footer.html/block区域解析。

1.在模板配置文件config/template.php中,填写以下代码 :

  1. // 开启全局模板布局
  2. 'layout_on' => false,
  3. // 全局模板配置文件
  4. 'layout_name' => 'layout',
  5. // 全局模板内容区定义,默认为'__CONTENT__'
  6. // 'layout_item' => '__TEXT__',

2.在view目录下新建public目录,下面新建base.html/header.html/footer.html文件。

  1. <!--基础模板base-->
  2. <!--模板头部-->
  3. {include file="public/header1" /}
  4. <!--区块-->
  5. {block name="body"}
  6. 主体
  7. {/block}
  8. <!--模板尾部-->
  9. {include file="public/footer1" /}

3.在View目录新建全局模板配置文件layout.html

  1. {include file='header' /}
  2. {__CONTENT__}
  3. {include file='footer' /}

4.在类对应的目录下,新建和方法同名的HTML文件,如test2.html

  1. <!--继承基础模板-->
  2. {extend name="public/base" /}
  3. <!--body区块的实现-->
  4. {block name="body"}
  5. {__block__}
  6. <h2>我是模板继承的案例</h2>
  7. {/block}
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class Demo8 extends Controller
  5. {
  6. public function test1()
  7. {
  8. return $this->view->fetch();
  9. }
  10. //模板继承
  11. public function test2()
  12. {
  13. return $this->view->fetch();
  14. }
  15. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!