Laravel框架Blade模板简介及模板继承用法分析

2025-05-27 0 74

本文实例讲述了Laravel框架Blade模板模板继承用法.分享给大家供大家参考,具体如下:

本章知识点主要如下:

  1. Blade模板简介
  2. Blade模板继承使用

NO.1Blade模板简介

问: 什么是Blade模板

答: Blade模板Laravel提供一个既简单又强大的模板引擎;
和其他流行的PHP模板引擎不一样,他并不限制你在视图里使用原生PHP代码;
所有Blade视图页面都将被编译成原生的PHP代码并缓存起来,除非你的模板文件被修改,否则不会重新编译。
而这些都意味着Blade不会给我们增加任何负担。

NO.2Blade模板继承使用

先说一下这里我们会用到的知识点

  1. section
  2. yield
  3. extends
  4. parent

问: Blade模板继承使用的优势在哪?为什么要使用它?

答:
Blade模板继承的优势在于,你写一个管理系统或者别的系统的时候,如果某部分样式不变,你可能会因为这个写一个又一个页面,就很麻烦,而且代码量多,做的时间久,别人接手也会抓狂,代码观赏性不强。但是你要是用到了Blade模板继承,你就可以省掉那些一样板块代码的数量;
为什么要使用它?因为方便维护,也节省代码量。 多说无益,我们拿出事实说话。

这里,我们先拿出一个Bootstrap的样式,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Bootstrap与Laravel的测试集合</title>
  6. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" >
  7. <script src="bootstrap/js/jquery.min.js"></script>
  8. <script src="bootstrap/js/bootstrap.min.js"></script>
  9. <style>
  10. .fakeimg {
  11. height: 200px;
  12. background: #aaa;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="jumbotron text-center" style="marginbottom:0">
  18. <h1>你好!这里是陈柴的系统</h1>
  19. <p>这里是Laravel与Bootstrap的集合</p>
  20. </div>
  21. <nav class="navbar navbar-inverse">
  22. <div class="container-fluid">
  23. <div class="navbar-header">
  24. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
  25. <span class="icon-bar"></span>
  26. <span class="icon-bar"></span>
  27. <span class="icon-bar"></span>
  28. </button>
  29. <a class="navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >网站名</a>
  30. </div>
  31. <div class="collapse navbar-collapse" id="myNavbar">
  32. <ul class="nav navbar-nav">
  33. <li class="@yield('index')"><a href="{{url('index')}}" rel="external nofollow" rel="external nofollow" >首页</a></li>
  34. <li class="@yield('login')"><a href="{{url('student')}}" rel="external nofollow" rel="external nofollow" >信息表</a></li>
  35. </ul>
  36. </div>
  37. </div>
  38. </nav>
  39. <div class="container">
  40. <div class="row">
  41. <div class="col-sm-4">
  42. <h2>关于我</h2>
  43. <h5>我的照片:</h5>
  44. <div class="fakeimg">这边插入图像</div>
  45. <p>关于我的介绍..</p>
  46. <h3>链接</h3>
  47. <p>描述文本。</p>
  48. <ul class="nav nav-pills nav-stacked">
  49. <li class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 1</a></li>
  50. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 2</a></li>
  51. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 3</a></li>
  52. </ul>
  53. <hr class="hidden-sm hidden-md hidden-lg">
  54. </div>
  55. <div class="col-sm-8">
  56. <h2>标题</h2>
  57. <h5>副标题</h5>
  58. <div class="fakeimg">图像</div>
  59. <p>一些文本..</p>
  60. <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  61. <br>
  62. <h2>标题</h2>
  63. <h5>副标题</h5>
  64. <div class="fakeimg">图像</div>
  65. <p>一些文本..</p>
  66. <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  67. </div>
  68. </div>
  69. </div>
  70. <div class="jumbotron text-center" style="marginbottom:0">
  71. <p>底部内容</p>
  72. </div>
  73. </body>
  74. </html>

当然了,如果你想要使用Bootstrap的框架,那你实现要把Bootstrap框架的文件下载好,然后存放于public目录下,才能使用。

然后我们在view目录下创建一个名为Bstp.blade.php的视图,将上面Bootstrap的代码复制过去。

做到这,我们继续在view目录下午创建一个目录,命名为Bstp,在往里面写入一个文件,命名为Bstp.blade.php

这个时候,我们就要思考怎么才能继承这个模板了。这个很简单,只需要用到上面我们提到的那几个单词知识点即可。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>@yield('title')</title>
  6. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" >
  7. <script src="bootstrap/js/jquery.min.js"></script>
  8. <script src="bootstrap/js/bootstrap.min.js"></script>
  9. <style>
  10. .fakeimg {
  11. height: 200px;
  12. background: #aaa;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. @section('jumbotron')
  18. <div class="jumbotron text-center" style="marginbottom:0">
  19. <h1>你好!这里是陈柴的系统</h1>
  20. <p>这里是Laravel与Bootstrap的集合</p>
  21. </div>
  22. @show
  23. @section('nav')
  24. <nav class="navbar navbar-inverse">
  25. <div class="container-fluid">
  26. <div class="navbar-header">
  27. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
  28. <span class="icon-bar"></span>
  29. <span class="icon-bar"></span>
  30. <span class="icon-bar"></span>
  31. </button>
  32. <a class="navbar-brand" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >网站名</a>
  33. </div>
  34. <div class="collapse navbar-collapse" id="myNavbar">
  35. <ul class="nav navbar-nav">
  36. <li class="@yield('index')"><a href="{{url('index')}}" rel="external nofollow" rel="external nofollow" >首页</a></li>
  37. <li class="@yield('login')"><a href="{{url('student')}}" rel="external nofollow" rel="external nofollow" >信息表</a></li>
  38. </ul>
  39. </div>
  40. </div>
  41. </nav>
  42. @show
  43. @section('box')
  44. <div class="container">
  45. <div class="row">
  46. <div class="col-sm-4">
  47. <h2>关于我</h2>
  48. <h5>我的照片:</h5>
  49. <div class="fakeimg">这边插入图像</div>
  50. <p>关于我的介绍..</p>
  51. <h3>链接</h3>
  52. <p>描述文本。</p>
  53. <ul class="nav nav-pills nav-stacked">
  54. <li class="active"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 1</a></li>
  55. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 2</a></li>
  56. <li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >链接 3</a></li>
  57. </ul>
  58. <hr class="hidden-sm hidden-md hidden-lg">
  59. </div>
  60. <div class="col-sm-8">
  61. <h2>标题</h2>
  62. <h5>副标题</h5>
  63. <div class="fakeimg">图像</div>
  64. <p>一些文本..</p>
  65. <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  66. <br>
  67. <h2>标题</h2>
  68. <h5>副标题</h5>
  69. <div class="fakeimg">图像</div>
  70. <p>一些文本..</p>
  71. <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  72. </div>
  73. </div>
  74. </div>
  75. @show
  76. @section('footer')
  77. <div class="jumbotron text-center" style="marginbottom:0">
  78. <p>底部内容</p>
  79. </div>
  80. @show
  81. </body>
  82. </html>

@section(‘nav')

@show

@show
这里代表的是一个继承某个代码块的开始以及结束,section开始,show结束,nav定义这个可以修改的代码块名字。方便子模板调用。

@yield(‘title')
这里和上面的定义差不多,唯一不同的是,他是不可扩展的,也就是说,原来这个div有多大,你就只能多大,而上面那个不一样,他的内容只要超过了原本的div,那么原本的div会随之增大

。@extends(‘Bstp')
这个代表着,你这个子模板继承于谁,我这里写的是这个子模板继承于view目录下的Bstp.blade.php。

@parent
这个代表着,把你原本的一起继承过来,覆盖。

说了这么多,如果还不理解,那咱们就行动证明

首先,我们验证第一个@extends

然后,打开我们view目录下的Bstp目录里的Bstp.blade.php文件,然后输入@extends,并且给他赋予一个控制器和路由

子模板代码如下:

  1. @extends('Bstp')//继承自view目录下的Bstp.blade.php

控制器代码如下:

  1. namespace App\\Http\\Controllers;
  2. class StudentController extends Controller
  3. {
  4. public function index()
  5. {
  6. return view('Bstp.Bstp');//这里指的是返回view目录下Bstp目录下的Bstp
  7. }
  8. }

路由如下:

  1. Route::get('index',['as'=>'index','uses'=>'StudentController@index']);

然后我们输入index,获得效果如下
Laravel框架Blade模板简介及模板继承用法分析
这里,我们是不是已经输出出来了?
(这里有个点值得注意,因为我在<title></title>里输入了@yield(‘title'),然后在,Bstp下又给他赋了个值,叫首页,所以标题就是首页)

然后如果我们想要把中间那块“关于我”,“标题”,“链接”,去掉怎么办?
好,那么我们只需要,在Bstp.blade.php文件里(Bstp下的),输入一个空的

  1. @section('box')
  2. @stop

即可,效果如下:
Laravel框架Blade模板简介及模板继承用法分析
Laravel框架Blade模板简介及模板继承用法分析
你们看,是不是没有了?
那么好,问题又来了,有的小伙伴想在原来的基础上再新增一点东西,能让这个不消失,而且也能显示新增的东西,要怎么办呢?
这个问题仅仅只需要一个@parent

如下:
Laravel框架Blade模板简介及模板继承用法分析
Laravel框架Blade模板简介及模板继承用法分析
你看,左下角是不是有个abc啊。

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

原文链接:https://blog.csdn.net/weixin_44596681/article/details/89202034

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Laravel框架Blade模板简介及模板继承用法分析 https://www.kuaiidc.com/71006.html

相关文章

发表评论
暂无评论