1. 繼承、片段、占位、組件、插槽
1.1 繼承 1、定義父模板 Laravel/resources/views/base.blade.php 2、子模板繼承 @extends('base')
1.2 片段 1、父模板定義片段 @section('part') // 中間內容即使是一個片段
@show
2、子模板填充片段 @section('part') // 片段填充內容(后台的表單、列表等)
@endsection
1.3 占位 1、父模板占位 @yield('title')
2、子模板填充占位 第一種填充(文本): @section('title' , '填充的文本占位') 第二種填充(文本 or html) @section('title') // 填充的占位
@endsection
1.4 組件、插槽 1、定義組件 // 路徑:Laravel/resources/views/component.blade.php
<div class='component'>
<!-- $title,$content 變量實際上就是預定義的插槽 -->
<div class='title'>{{ $title }}</div>
<div class='content'>{{ $content }}</div>
</div>
2、使用組件 // 路徑:Laravel/resources/views/test.blade.php
@component('component') @slot('title') 組件標題 @endsolt @slot('content') 組件內容 @endslot @endcomponent
2.數據顯示 2.1 轉義輸出 {{ $name }}
2.2 未轉義輸出 {!! $name !!}
2.3 打印內容並帶一個默認值 {{ $var or 'default' }}
2.4 原格式輸出 // 第一種(適合量不多):
@{{ name }} // 第二種(適合量多):
@verbatim {{ name }} {{ sex }} {{ age }} @endverbatim
3. 流程控制
3.1 for @for ($i = 0; $i < 10; ++$i) {{ $i }} <br /> @endfor
3.2 foreach @foreach ($data as $k => $v) {{ $k }} <br /> @endforeach
3.3 forelse @foreach ($data as $k => $v) // $data有值
{{ $k }} <br /> @empty
// $data沒有值
@endforeach
3.4 if @if(condition) - if開始 @else - else不帶條件 @elseif(condition) - else帶條件 @endif - 結束if
3.5 while @while(condition) - while循環開始 @endwhile - while循環結束
3.6 unless @unless(condition) - unless開始 @endunless - unless結束
4. 使用原生 PHP @php // 里面寫php代碼
echo "使用原生 PHP"; @endphp
5. 包含子視圖 被包含的子視圖可以引用父視圖定義的所有變量。 你可以傳遞額外的數據到子視圖 定義父視圖 parent.blade.php,並包含子視圖 child.blade.php,且傳入額外數據 <!-- 包含子視圖 --> @include("child" , [ "other" => "額外數據" ])