PHP - Laravel 视图模板(blade.php) 模板继承(@extends、@yield、@section)
发布日期:2021-06-29 11:38:02 浏览次数:3 分类:技术文章

本文共 1069 字,大约阅读时间需要 3 分钟。

一、简介

  • 当一个页面的 头部尾部 完全一样,只要中间内容不一致,这个时候需要用到一个头尾公共的模板,然后其他页面多继承于这个公共模板编写中间内容部分即可。

  • 模板继承格式

    // components 表示在 views 下面的 components 文件夹// main 表示在 views/components/main.blade.php 文件@extends('components.main')

二、简单继承使用

  • 新建一个 main.blade.php 作为公共视图文件

      @include('components.header')  页面内容  @include('components.footer')
  • 然后新建 header.blade.phpfooter.blade.php 文件

    我是头部
    我是尾部

    image.png

  • 然后使用 index.blade.php 继承 main.blade.php 文件

    image.png

    image.png

三、@yield 占位符,@section 输出符

  • 根据页面需求自定义公共模板中的内容,例如:页面内容,上面之前是写死内容,现在需要每个页面显示不同类容。

  • main.blade.php 文件

      
    {
    {-- 自定义标题 --}} @yield('title') {
    {-- 自定义CSS --}} @yield('css') @include('components.header') {
    {-- 自定义中间内容 --}} @yield('content') @include('components.footer') {
    {-- 自定义JS --}} @yield('js')
  • index.blade.php 文件

    {
    {-- 继承 --}}@extends('components.main'){
    {-- 用法一:单标签 --}}@section('title', 'DzmTest'){
    {-- 用法二:双标签 --}}@section('content')
    我是页面内容
    @endsection{
    {-- css --}}@section('css') @endsection{
    {-- js --}}@section('js') @endsection
  • Demo效果

    image.png

转载地址:https://blog.csdn.net/zz00008888/article/details/115179869 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:PHP - Laravel 表单验证(验证规则与使用 $this->validate()、Validator::make()、Requests)
下一篇:nginx: [emerg] duplicate location “/“ in /usr/local/etc/nginx/nginx.conf:142

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月23日 05时41分40秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章