模板布局,如头部和底部公共部分的替换

一、局部模板布局

里边这段代码不需要加入,多余的
‘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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值