Blade視圖檔案使用 '.blade.php' 作為字尾,模闆檔案都儲存在 'resources/views'
[email protected]('content')
2.
@section('sidebar')
@show
3.
@extends('laytouts.master') // 繼承 layouts/master.blade.php
4.
@section('content', '要展示的内容') // 簡單的區塊替換,直接使用字元串
5.
// 複雜的區塊,使用段落形式
@section('sidebar')
@endsection
6.
@section('sidebar')
@parent
@endsection
// @section('sidebar')&@show區塊組合,可使用@parent來擷取,然後使用同一個section作為擴充
7.
view('welcome', ['name' => 'dongxuemin']) // 在路由中使用view()來調用Blade模闆,并配置設定變量
8.模闆檔案中,模闆标記為 {{ }}(會自動調用php的 'htmlentities()' 轉實體)
9.當模闆中的javascript架構,也具有模闆解析功能,例如:AngularJS,也使用 '{{}}' 解析模闆。Blade可以使用 '@{{}}'(當然AngularJS也允許修改左右定界符'{{}}')
10.{{ isset($name) ? $name : 'Default'}},Blade提供了簡寫方法:{{ $name or 'Default'}}
11.if結構
@if ()
@elseif ()
@else
@endif
12.unless結構,基本沒用,等同于 @if(!xxx) @endif
@unless ()
@endunless
13.for循環
@for ()
@endfor
14.foreach循環
@foreach
@endforeach
15.while循環
@while()
@endwhile
16.foreach循環的變種,當循環數組 == false(boolean轉換為false) 時,可采用這種結構
@forelse($users as $user)
<li>使用者名:{{$user['username']}}</li>
@empty
<p>沒有使用者</p>
@endforelse
[email protected]('shared.errors', ['name'] => '董學敏') // 引入子視圖,并給子視圖配置設定變量
18.為數組渲染視圖,可了解為循環數組的另外一種方式
@each('view.user', $users, $user[, 'view.empty'])
參數解釋:
- 'view.user'每個條目将要展示的模闆
- $users - 循環數組
- $user - 在 'view.user' 中,可使用的變量(即:一條使用者資料)
- 'view.empty',可選參數。當數組為空時,将會渲染的模闆
可以看出,完全可以使用 '@forelse' 替代
19.注釋:
{{-- 我是注釋内容,不會被html展示,我們項目中自己可見注釋! --}}
20.服務注入:
@inject('users', 'App\Services\usersService')
參數解釋:
- 服務的變量名稱(配置設定給模闆使用)
- 解析服務的類或是接口的名稱
模闆中使用,假設 $users->$userList 為一個使用者清單:
@forelse($userList as $user)
<li>使用者名:{{$user['username']}}</li>
@empty
<p>沒有使用者</p>
@endforelse
21.擴充Blade - 進階用法,不做解釋