- Blade视图文件使用.blade.php文件扩展并存放在resources/views目录下。
- routes.php
Route::get('/child', function () {
$records=['a','b','c'];
return view('layouts/child',[
'firstStr'=>'hello child h3 firstStr',
'name'=>'name hello',
'records'=>$records
]);
});
master.blade.php
<h3>@yield('firstStr')</h3>
<h3>{{ time() }}</h3>
<h3>{{ isset($name) ? $name : 'Default' }}</h3>
<h3>
@if (count($records) === 1)
I have one record!
@elseif (count($records) > 1)
I have multiple records!
@else
I don't have any records!
@endif
</h3>
<div class="container">
@yield('content')
</div>
child.blade.php
@extends('layouts.master')
@section('firstStr')
{{ $firstStr }}
@endsection
@section('content')
<p>This is my body content.</p>
<h3>
@inject('metrics', 'App\Services\Hello')
<div>
Monthly Revenue: {{ $metrics->haha() }}.
</div>
</h3>
@endsection
App/Services/Hello.php
<?php
namespace App\Services;
class Hello
{
public function haha()
{
return "Services Hello haha";
}
}
- 循环
//
@for ($i = 0; $i < 10; $i++)
The current value is {{ $i }}
@endfor
//
@foreach ($users as $user)
<p>This is user {{ $user->id }}</p>
@endforeach
//
@forelse ($users as $user)
<li>{{ $user->name }}</li>
@empty
<p>No users</p>
@endforelse
//
@while (true)
<p>I'm looping forever.</p>
@endwhile
- 注释
{{-- This comment will not be present in the rendered HTML --}}
- 子视图@include
- 服务注入