一、模版继承
在一个项目中有许多模板文件,它们有一个特点:拥有共同的头部 和 脚部内容
为了避免相同代码重复开发、维护 造成工作效率低下
可以把共同的头部和脚部内容集中到一个布局文件中,之后各个具体模板文件去继承该布局文件而使用头部和脚步内容。这个过程成为模板继承。
布局文件中相同的代码只维护一份,会大大提升项目开发效率
模板文件:共有结构
注:模板文件中不确定的内容用@yield(‘占位标志xxx’)临时占位,在继承文件中用@section(‘占位标志xxx’,’真实内容’)完善补充
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>
{{--标题内容区域--}}
@yield('title')
</title>
</head>
<body>
<div style="background:red">我是头部</div>
<!--占位符-->
@yield('content')
<div style="background:green">我是底部</div>
</body>
</html>
实现继承:
@extends(‘laoyouts.home’)
#表示继承resource/views/layouts/home.blade.php布局文件
使用section标签替换布局模板中可变区域;
@section(‘content’)
给布局文件yield(‘content ‘)的区域进行填充的内容
@endsection
<!--继承父模板-->
@extends('public.layout')
<!--标题-->
@section('title','我是ext1模板')
<!--引入占位符区域-->
@section('content')
<div style="background:blue;">我是内容区域</div>
@endsection
二、模板包含
用法
@include(‘public.header’)
# public 表示 views下面的public目录
# header 表示在views/public/header.blade.php文件
被包含内容
<div style="background:silver;color:red;">
{{$error_msg}}
</div>
包含文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
{{--模板包含--}}
@include('public._error_msg')
<form action="">
账号:<input type="text"><br>
密码: <input type="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
个人感悟:模板的继承用于网页结构(大)的重复引用,模板的包含用于小段内容(小)的重复引用。