--- tags: laravel --- # 視圖的Blade版型(快速手冊)  ## Blade指令 ### 樣板控制 #### 挖洞 ``` @yield(‘{section name}’) @section(‘{section name}’) <div>可以選擇性繼承的內容</div> @show ``` #### 填洞 ``` 填單一內容使用 @section(‘{yield name}’ , ’{ string }’) 填複雜內容使用 @section(‘{yield name}’) @stop 包含 @include(‘{view name}’,['key1'=>'value1','key2'=>'value2']) 繼承,通常寫在子視圖的頭 @extends(‘{view name}’) ``` ### 程式邏輯控制 #### 條件判斷式 ``` 假如 @if @elseif @else @endif 除非 @unless @endunless 當 @switch @endswitch ``` #### 迴圈 ``` @foreach( $tasks as $task ) 逐一取出陣列的內容來加以處理 @endforeach @for( $i = 0 ; $i<10 ; $++ ) @endfor @forelse( $tasks as $task ) @empty 目前沒有工作 @endforelse @while @endwhile ``` ##### 隱含變數 $loop $loop->index 索引值,從0開始 $loop->iteration 第幾個,從1開始 $loop->remaining 還剩幾個 $loop->count 總共幾個 $loop->last 是否為最後 $loop->first 是否為第一 ### 輸出內容 {{ 內容遇到程式碼會當純文字處理 }} {!! 內容遇到程式碼會渲染並執行 !!} {{- - 註解 --}} @{{ 顯示Blade語法 }} ## 課程範例 ``` //resources/views/master.blade.php <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title> @yield('title') </title> </head> <body> @yield('content') @section('js') <script src="js/1.js" /> @show <p>js結束</p> </body> </html> //resources/views/index.blade.php @extends('layouts.master') @section('js') <script src="2.js" /> @parent @stop @section('title','首頁') @section('content') <div class="container clearfix"> 我的內容 </div> @foreach ($data as $tmp) <h2>{{ $tmp }}</h2> <h3>{{ $loop->index }} </h3> @endforeach @stop //routes/web.php Route::get('/index','HomeController@index'); //app/Http/Controllers/HomeController.php public function index() { return view('index'); } ``` ## 多視圖共享 ``` //全域共享 view()->share('tel','0212345678'); //部分共享 view()->composer(['items.*'],function($view){ $view->with('address','中山路一號'); }); ``` --- ### 參考資料 [Laravel 8](https://learninglaravel.net/cheatsheet/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up