一、局部模板布局
里边这段代码不需要加入,多余的
‘layout_on’ => true,
‘layout_name’=>‘layout’,
控制器
<?php
namespace app\index\controller;
use think\Controller;
class Demo8 extends Controller
{
public function test1()
{
return $this->view->fetch();
}
}
视图引入模板文件
{include file="header" /}
<h2>我是网站的主体部分</h2>
{include file="footer" /}
模板文件位置位于index模块下的view层
二、全局模板布局方法
模板布局适用于首部和尾部基本上不发生变化的网页,比如企业网站等
要使用模板布局,首先需要在application\config\template.php中开启layout
‘layout_on’ => true 这设置开启模板布局
‘layout_name’=>‘layout’, 设置模板布局名字为layout.html
设置模板布局
当开启模板布局之后,视图的渲染就不再是直接渲染到控制器方法对应的html文件了,而是会先去访问view/layout.html文件,根据layout.html做出相应的渲染动作
layout.html文件:application\index\view\layout.html
{include file='header' /}
{__CONTENT__}
{include file='footer' /}
header.html文件:application\index\view\header.html,对应{include file=‘header’ /}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2 style="color:red">我是网站的头部</h2>
footer.html文件:application\index\view\footer.html,对应{include file=‘footer’ /}
<h2 style="color:green">我是网站的底部</h2>
</body>
</html>
__CONTENT__所替换的内容对应的文件:application\index\view\demo8\test1.html,对应{CONTENT}
h2>我是网站的主体部分</h2>
控制器
<?php
namespace app\index\controller;
use think\Controller;
class Demo8 extends Controller
{
public function test1()
{
return $this->view->fetch();
}
}