Blade模版简介
- Blade模版的好处:
- 模版继承(template inheritance)
- 视图片段(sections)
部分指令:
- @extend(‘xxx’)为子页面指定所继承的页面布局模版
- @section(‘xxx’)为子页面提供所继承的页面中指定的部分
- @yield(‘xxx’)为布局模版指定部分,供section使用,可以声明多个
创建布局模版
- 如:我们在视图目录(/resources/views)中创建一个布局模版,这里我们命名为main.blade.php,这里是模版布局的代码
-
Document
@yield(‘content’)
- 在view目录下的about.blade.php中使用上述模版,具体代码如下
@extends(‘main’)
@section(‘content’)
About me
{{ $name}}
@stop
在Blade模版中调用php方法
- if表达式
@extends(‘main’)
@section(‘content’)
@if ($first == ‘John’)
Hi, John
@else
Else
@endif
@stop- 除了@if外,还可以使用@unless(相当于 if !)、@foreach、@forelse(@foreach循环数组为空时)等。
@extends(‘main’)
@section(‘content’)
About
<h3>People I Like:</h3><ul>@foreach ($people as $person)<li>{{ $person }}</li>@endforeach</ul>
@stop
- 当foreache中的数组为空是,可以加个判断if来处理
@extends(‘main’)
@section(‘content’)
About
@if (count($people))
People I Like:
@foreach (peopleasperson)
- {{ $person }}
- @endforeach
@endif
@stop