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


Posted in PHP onDecember 03, 2019

本文实例讲述了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的样式,代码如下:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"> 
	<title>Bootstrap与Laravel的测试集合</title>
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" > 
	<script src="bootstrap/js/jquery.min.js"></script>
	<script src="bootstrap/js/bootstrap.min.js"></script>
	<style>
  .fakeimg {
    height: 200px;
     background: #aaa;
  }
 </style>
</head>
<body>

<div class="jumbotron text-center" style="margin-bottom:0">
 <h1>你好!这里是陈柴的系统</h1>
 <p>这里是Laravel与Bootstrap的集合</p> 
</div>

<nav class="navbar navbar-inverse">
 <div class="container-fluid">
  <div class="navbar-header">
   <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>        
   </button>
   <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>
  </div>
  <div class="collapse navbar-collapse" id="myNavbar">
   <ul class="nav navbar-nav">
    <li class="@yield('index')"><a href="{{url('index')}}" rel="external nofollow" rel="external nofollow" >首页</a></li>
    <li class="@yield('login')"><a href="{{url('student')}}" rel="external nofollow" rel="external nofollow" >信息表</a></li>
   </ul>
  </div>
 </div>
</nav>

<div class="container">
 <div class="row">
  <div class="col-sm-4">
   <h2>关于我</h2>
   <h5>我的照片:</h5>
   <div class="fakeimg">这边插入图像</div>
   <p>关于我的介绍..</p>
   <h3>链接</h3>
   <p>描述文本。</p>
   <ul class="nav nav-pills nav-stacked">
    <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>
    <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>
    <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>
   </ul>
   <hr class="hidden-sm hidden-md hidden-lg">
  </div>
  <div class="col-sm-8">
   <h2>标题</h2>
   <h5>副标题</h5>
   <div class="fakeimg">图像</div>
   <p>一些文本..</p>
   <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
   <br>
   <h2>标题</h2>
   <h5>副标题</h5>
   <div class="fakeimg">图像</div>
   <p>一些文本..</p>
   <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  </div>
 </div>
</div>

<div class="jumbotron text-center" style="margin-bottom:0">
 <p>底部内容</p>
</div>
</body>
</html>

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

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

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

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

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"> 
	<title>@yield('title')</title>
	<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" rel="external nofollow" rel="external nofollow" > 
	<script src="bootstrap/js/jquery.min.js"></script>
	<script src="bootstrap/js/bootstrap.min.js"></script>
	<style>
  .fakeimg {
    height: 200px;
     background: #aaa;
  }
 </style>
</head>
<body>

@section('jumbotron')
<div class="jumbotron text-center" style="margin-bottom:0">
 <h1>你好!这里是陈柴的系统</h1>
 <p>这里是Laravel与Bootstrap的集合</p> 
</div>
@show

@section('nav')
<nav class="navbar navbar-inverse">
 <div class="container-fluid">
  <div class="navbar-header">
   <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>        
   </button>
   <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>
  </div>
  <div class="collapse navbar-collapse" id="myNavbar">
   <ul class="nav navbar-nav">
    <li class="@yield('index')"><a href="{{url('index')}}" rel="external nofollow" rel="external nofollow" >首页</a></li>
    <li class="@yield('login')"><a href="{{url('student')}}" rel="external nofollow" rel="external nofollow" >信息表</a></li>
   </ul>
  </div>
 </div>
</nav>
@show

@section('box')
<div class="container">
 <div class="row">
  <div class="col-sm-4">
   <h2>关于我</h2>
   <h5>我的照片:</h5>
   <div class="fakeimg">这边插入图像</div>
   <p>关于我的介绍..</p>
   <h3>链接</h3>
   <p>描述文本。</p>
   <ul class="nav nav-pills nav-stacked">
    <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>
    <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>
    <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>
   </ul>
   <hr class="hidden-sm hidden-md hidden-lg">
  </div>
  <div class="col-sm-8">
   <h2>标题</h2>
   <h5>副标题</h5>
   <div class="fakeimg">图像</div>
   <p>一些文本..</p>
   <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
   <br>
   <h2>标题</h2>
   <h5>副标题</h5>
   <div class="fakeimg">图像</div>
   <p>一些文本..</p>
   <p>菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!菜鸟教程,学的不仅是技术,更是梦想!!!</p>
  </div>
 </div>
</div>
@show

@section('footer')
<div class="jumbotron text-center" style="margin-bottom:0">
 <p>底部内容</p>
</div>
@show
</body>
</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,并且给他赋予一个控制器和路由

子模板代码如下:

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

控制器代码如下:

namespace App\Http\Controllers;

class StudentController extends Controller
{
	public function index()
	{
		return view('Bstp.Bstp');//这里指的是返回view目录下Bstp目录下的Bstp
	}
}

路由如下:

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

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

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

@section('box')

@stop

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

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

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

PHP 相关文章推荐
一个用php实现的获取URL信息的类
Jan 02 PHP
PHP通用分页类page.php[仿google分页]
Aug 31 PHP
php中jQuery插件autocomplate的简单使用笔记
Jun 14 PHP
PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
Jun 12 PHP
PHP清除字符串中所有无用标签的方法
Dec 01 PHP
PHP整合PayPal支付
Jun 11 PHP
PHP+Jquery与ajax相结合实现下拉淡出瀑布流效果【无需插件】
May 06 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
Apr 10 PHP
thinkphp实现附件上传功能
May 26 PHP
PHP设计模式之工厂模式详解
Oct 24 PHP
PHP实现随机发放扑克牌
Apr 21 PHP
Thinkphp集成抖音SDK的实现方法
Apr 28 PHP
在phpstudy集成环境下的nginx服务器下配置url重写
Dec 02 #PHP
php 实现简单的登录功能示例【基于thinkPHP框架】
Dec 02 #PHP
关于Yii2框架跑脚本时内存泄漏问题的分析与解决
Dec 01 #PHP
详解no input file specified 三种解决方法
Nov 29 #PHP
设定php简写功能的方法
Nov 28 #PHP
如何在centos8自定义目录安装php7.3
Nov 28 #PHP
PHP的new static和new self的区别与使用
Nov 27 #PHP
You might like
使用sockets:从新闻组中获取文章(二)
2006/10/09 PHP
apache rewrite_module模块使用教程
2008/01/10 PHP
PHP读取XML值的代码(推荐)
2011/01/01 PHP
模板引擎正则表达式调试小技巧
2011/07/20 PHP
PHP之密码加密的几种方式
2015/07/29 PHP
OAuth认证协议中的HMACSHA1加密算法(实例)
2017/10/25 PHP
PHP实现微信支付(jsapi支付)流程步骤详解
2018/03/15 PHP
服务端 VBScript 与 JScript 几个相同特性的写法 By shawl.qiu
2007/03/06 Javascript
xheditor与validate插件冲突的解决方案
2010/04/15 Javascript
jquery each()源代码
2011/02/14 Javascript
10款非常有用的 Ajax 插件分享
2012/03/14 Javascript
JavaScript获取当前网页标题(title)的方法
2015/04/03 Javascript
JavaScript创建闭包的两种方式的优劣与区别分析
2015/06/22 Javascript
深入浅析AngularJS和DataModel
2016/02/16 Javascript
Angular的$http与$location
2016/12/26 Javascript
微信JSAPI Ticket接口签名详解
2020/06/28 Javascript
页面缩放兼容性处理方法(zoom,Firefox火狐浏览器)
2017/08/29 Javascript
基于jquery.page.js实现分页效果
2018/01/01 jQuery
浅析vue插槽和作用域插槽的理解
2019/04/22 Javascript
python根据出生日期获得年龄的方法
2015/03/31 Python
pygame学习笔记(6):完成一个简单的游戏
2015/04/15 Python
python实现class对象转换成json/字典的方法
2016/03/11 Python
Python cookbook(数据结构与算法)保存最后N个元素的方法
2018/02/13 Python
Python回文字符串及回文数字判定功能示例
2018/03/20 Python
Django 实现购物车功能的示例代码
2018/10/08 Python
Python Excel处理库openpyxl使用详解
2019/05/09 Python
python设计tcp数据包协议类的例子
2019/07/23 Python
python3.7实现云之讯、聚合短信平台的短信发送功能
2019/09/26 Python
python生成并处理uuid的实现方式
2020/03/03 Python
Python 解析库json及jsonpath pickle的实现
2020/08/17 Python
flask项目集成swagger的方法
2020/12/09 Python
利群广告词
2014/03/20 职场文书
奥巴马的演讲稿
2014/05/15 职场文书
创先争优活动心得体会
2014/09/04 职场文书
乡镇2014法制宣传日活动总结
2014/11/01 职场文书
校园之声广播稿
2015/08/18 职场文书