①Blade是Laravel提供的一个简单强大的模板引擎。
②和其他流行的PHP模板引擎不一样,Blade并不现在你在视图view中使用原生PHP代码。
③所有Blade视图页面都将被编译成原生PHP代码并缓存起来,除非模板文件被修改,否则不会被重写编译。
本博文主要记录了如下功能点:
@yield
@section
@extends
@stop
@parent
@include和传参
@if
@elseif
@else
@endif
@unless
@endunless
@for
@endfor
@foreach
@endforeach
@forelse
@empty
@endforelse
以及URL
程序结构如下:
路由web.php添加:
- Route::prefix('student')->group(function(){
-
- ......
- ......
- ......
-
- Route::get('section1', 'StudentController@section1');
- Route::get('urlTest', 'StudentController@urlTest')->name('url');
- });
url.blade.php
- <a href="{{url('url')}}">url()</a>
- <br/>
- <a href="{{action('StudentController@urlTest')}}">action()</a>
- <br/>
- <a href="{{route('url')}}">route()</a>
layouts.blade.php
- <!DOCTYPE html>
- <html lang="en">
- <meta charset="utf-8">
- <title>轻松学会Laravel - @yield('title')</title>
- <style>
- .header{
- width:1000px;
- height:150px;
- margin:0 auto;
- background:#f5f5f5;
- border: 1px solid #ddd;
- }
- .main{
- width:1000px;
- height:300px;
- margin:0 auto;
- clear:both;
- margin-top:15px;
- }
- .main .sidebar{
- float:left;
- width:20%;
- height: inherit;
- background: #f5f5f5;
- border: 1px solid #ddd;
- }
- .main .content{
- float:right;
- width:75%;
- height:inherit;
- background:#f5f5f5;
- border: 1px solid #ddd;
- }
- .footer{
- width:1000px;
- height:150px;
- margin:0 auto;
- margin-top:15px;
- background:#f5f5f5;
- border:1px solid #ddd;
- }
- </style>
- <body>
- <div class = "header">
- @section('header')头部@show
- </div>
- <div class = "main">
- <div class = "sidebar">
- @section('sidebar')
- 侧边栏
- @show
- </div>
- <div class = "content">
- @yield('content','主要内容区域')
- </div>
- </div>
- <div class = "footer">
- @section('footer')
- 底部
- @show
- </div>
- </body>
- </html>
common1.php
<p>我是include {{$message}}</p>
section1.blade.php
- @extends('layouts')
-
- @section('title')
- 哼哼哈嘿
- @stop
-
- @section('header')
- @parent
- header
- @stop
-
- @section('sidebar')
- @parent
- sidebar
- @stop
-
- @section('content')
- content
- <!-- 模板输出php变量 -->
- <p>{{$name}}</p>
-
- <!-- 模板调用php代码 -->
- <p>{{time()}}</p>
- <p>{{date('Y-m-d H:i:s', time())}}</p>
- <p>{{in_array($name, $arr) ? 'true' : 'false'}}</p>
- <p>{{var_dump($arr)}}</p>
-
- <p>{{isset($name) ? $name : 'default'}}</p>
- <p>{{$name or 'default'}}</p>
-
- @include('student.common1', ['message' => '我是错误信息'])
- <br/>
-
- @if($name == '小明')
- 我是小明
- @elseif($name == '小黑')
- 我是小黑
- @else
- 我是谁
- @endif
-
- <!-- if中可以用php函数 -->
- <br/>
- @if (in_array($name, $arr))
- true
- @else
- false
- @endif
-
- <!-- unless的使用,属于if的取反 -->
- <br/>
- @unless($name != '小明')
- 我是小明
- @endunless
-
- @unless($name == '小明')
- 我不是小明
- @endunless
-
- {{-- @for的使用--}}
- <br/>
- @for($i = 0; $i < 10; $i++)
- <p>{{$i}}</p>
- @endfor
-
- {{-- @foreach的使用--}}
- <br/>
- @foreach($students as $student)
- <p>{{$student->name}}</p>
- @endforeach
-
- {{-- foreach的变种forelse--}}
- @forelse($students as $student)
- <p>{{$student->name}}</p>
- @empty
- <p>null</p>
- @endforelse
-
- @stop
StudentController.php关键代码如下:
- <?php
-
-
- namespace App\Http\Controllers;
-
-
- use App\Student;
- use Illuminate\Support\Facades\DB;
-
- class StudentController extends Controller{
-
- ......
- ......
-
- public function section1(){
-
- $students = Student::get();
-
- $name = '小明';
- $arr = ['小白', '小黑'];
- return view('student.section1', [
- 'name' => $name,
- 'arr' => $arr,
- 'students' => $students
- ]);
- }
-
- public function urlTest(){
-
- return view("url");
- }
- }