extends,includes,yield,section,这些blade模板里面的标签分别怎么用?extends,includes有什么区别?yield,section有什么区别?
extends
、includes
、@yield
、@section
和@stop
/@endsection
1. @extends
和 @include
@extends
@extends
用于指定当前视图继承自另一个视图(通常称为布局或母版视图)。这允许你定义网站或应用的基础HTML结构(如头部、导航栏和页脚),然后在不同的视图中填充内容。
用法示例:
@extends('layouts.app')
@section('content')
<!-- 页面特定内容 -->
@endsection
@include
@include
用于在当前视图中包含一个其他视图的内容。这通常用于复用页面片段,如侧边栏、导航链接等。
用法示例:
<div class="sidebar">
@include('partials.sidebar')
</div>
区别:
-
@extends
用于定义视图的继承关系,主要目的是复用整个页面的结构。 -
@include
用于在当前视图中包含其他视图的片段,主要目的是复用页面上的小块内容。
2. @yield
和 @section
/@endsection
@yield
@yield
通常与@extends
一起使用,在布局文件中指定一个或多个内容区域,这些区域将被子视图中的相应@section
填充。但是,直接在你的子视图中使用@yield
是不常见的,因为@yield
更多地是在布局文件中用来定义插槽(slot)。
布局文件中使用@yield
示例:
<!DOCTYPE html>
<html>
<head>
<title>App Name - @yield('title')</title>
</head>
<body>
@yield('content')
</body>
</html>
@section
和 @endsection
@section
和@endsection
用于在子视图中定义要传递到布局视图的内容块。这些内容块将填充布局文件中通过@yield
指定的插槽。
子视图中使用@section
和@endsection
示例:
@extends('layouts.app')
@section('title', '页面标题')
@section('content')
<!-- 页面特定内容 -->
@endsection
注意: @yield
本身不直接在子视图中使用,而是在布局文件中定义内容插槽。@section
和@endsection
在子视图中定义要填充到这些插槽的内容。
总结
-
@extends
和@include
分别用于视图的继承和内容的包含。 -
@yield
在布局文件中定义内容插槽,而@section
和@endsection
在子视图中定义要填充到这些插槽的内容。 -
所有用户都可以去薅羊毛,192元充值200元话费!先到先得!导航栏话费充值,正规可靠,快充慢充自由选择。
欧阳逸资源站 » extends,includes,yield,section,这些blade模板里面的标签分别怎么用?extends,includes有什么区别?yield,section有什么区别?
欧阳逸资源站 » extends,includes,yield,section,这些blade模板里面的标签分别怎么用?extends,includes有什么区别?yield,section有什么区别?