目次
Laravelの記事一覧は下記
PHPフレームワークLaravelの使い方
Laravelバージョン
動作確認はLaravel Framework 7.19.1で行っています
Laravelの描画処理
Laravelの描画処理を行うbladeテンプレートエンジンの使い方を書いていきます
@と様々な予約語を組み合わせることでいろいろな描画処理をすることができます
前提条件
eclipseでLaravel開発環境を構築する。デバッグでブレークポイントをつけて止める。(WindowsもVagrantもdockerも)
本記事は上記が完了している前提で書かれています
プロジェクトの作成もapacheの設定も上記で行っています
Controllerにメソッド追加
-
/sample/app/Http/Controllers/SampleController.phpにtplメソッドを追記
public function tpl() { $data = ['val' => 2, 'parentList' => ['key1' => 1], 'list' => ['key1' => 1, 'key2' => 2, 'key3' => 3], 'emptyList' => [] ]; return view('sample.tpl', $data); }
-
/sample/routes/web.phpに下記を追記
Route::get('sample/tpl', 'SampleController@tpl');
viewの作成
- /sample/resources/views/sample/tpl.blade.phpファイル作成
<html>
<head>
<title>sample</title>
</head>
<body>
{{-- コメント --}}
{{-- {!! !!}はエスケープしないデータの表示 --}}
<div>[{!! '@php' !!}]</div>
@php
$tplVal = 10;
echo '<div>' . $tplVal . '</div>';
@endphp
<br>
<div>[echo]</div>
<div>{{ $val }}</div>
<br>
<div>[if]</div>
@if ($val === 1)
<div>1</div>
@elseif ($val === 2)
<div>2</div>
@else
<div>else</div>
@endif
<br>
<div>[switch]</div>
@switch($val)
@case(1)
<div>1</div>
@break
@case(2)
<div>2</div>
@break
@default
<div>default</div>
@endswitch
<br>
<div>[unless]</div>
@unless (true)
<div>true</div>
@else
<div>false</div>
@endunless
<br>
<div>[isset]</div>
@isset($list['key1'])
<div>isset true</div>
@else
<div>isset false</div>
@endisset
<br>
<div>[empty]</div>
@empty($list['key1'])
<div>empty true</div>
@else
<div>empty false</div>
@endempty
<br>
<div>[for]</div>
@for ($i = 0; $i < 10; $i++)
@if ($i == 1)
@continue
@endif
<div>{{ $i }}</div>
@if ($i >= 3)
@break
@endif
@endfor
<br>
<div>[foreach]</div>
@foreach ($list as $listKey => $listVal)
@if ($listVal == 1)
@continue
@endif
<div>{{ $listKey }}:{{ $listVal }}</div>
@if ($listVal >= 3)
@break
@endif
@endforeach
<br>
<div>[forelse]</div>
@forelse ($emptyList as $listKey => $listVal)
<div>{{ $listKey }}:{{ $listVal }}</div>
@empty
<div>空っぽのリストです</div>
@endforelse
<br>
<div>[while]</div>
@php
$whileIndex = 0;
@endphp
@while ($whileIndex <= 3)
<div>{{ $whileIndex }}</div>
@php
$whileIndex++;
@endphp
@endwhile
<br>
<div>[$loop]</div>
@foreach ($parentList as $parentListKey => $parentListVal)
<div>parent:{{ $parentListKey }}:{{ $parentListVal }}</div>
@foreach ($list as $listKey => $listVal)
<div>child:{{ $listKey }}:{{ $listVal }}</div>
<div>$loop->index: {{ $loop->index }}</div>
<div>$loop->iteration:{{ $loop->iteration }}</div>
<div>$loop->remaining:{{ $loop->remaining }}</div>
<div>$loop->count: {{ $loop->count }}</div>
<div>$loop->first: {{ $loop->first }}</div>
<div>$loop->last: {{ $loop->last }}</div>
<div>$loop->even: {{ $loop->even }}</div>
<div>$loop->odd: {{ $loop->odd }}</div>
<div>$loop->depth: {{ $loop->depth }}</div>
<div>$loop->parent->index: {{ $loop->parent->index }}</div>
<div>$loop->parent->iteration:{{ $loop->parent->iteration }}</div>
<div>$loop->parent->remaining:{{ $loop->parent->remaining }}</div>
<div>$loop->parent->count: {{ $loop->parent->count }}</div>
<div>$loop->parent->first: {{ $loop->parent->first }}</div>
<div>$loop->parent->last: {{ $loop->parent->last }}</div>
<div>$loop->parent->even: {{ $loop->parent->even }}</div>
<div>$loop->parent->odd: {{ $loop->parent->odd }}</div>
<div>$loop->parent->depth: {{ $loop->parent->depth }}</div>
@if ($listVal >= 2)
@break
@endif
@endforeach
@endforeach
<br>
</body>
</html>
動作確認
http://localhost/laravelSample/sample/tpl
実行結果
[@php]
10
[echo]
2
[if]
2
[switch]
2
[unless]
false
[isset]
isset true
[empty]
empty false
[for]
0
2
3
[foreach]
key2:2
key3:3
[forelse]
空っぽのリストです
[while]
0
1
2
3
[$loop]
parent:key1:1
child:key1:1
$loop->index: 0
$loop->iteration:1
$loop->remaining:2
$loop->count: 3
$loop->first: 1
$loop->last:
$loop->even:
$loop->odd: 1
$loop->depth: 2
$loop->parent->index: 0
$loop->parent->iteration:1
$loop->parent->remaining:0
$loop->parent->count: 1
$loop->parent->first: 1
$loop->parent->last: 1
$loop->parent->even:
$loop->parent->odd: 1
$loop->parent->depth: 1
child:key2:2
$loop->index: 1
$loop->iteration:2
$loop->remaining:1
$loop->count: 3
$loop->first:
$loop->last:
$loop->even: 1
$loop->odd:
$loop->depth: 2
$loop->parent->index: 0
$loop->parent->iteration:1
$loop->parent->remaining:0
$loop->parent->count: 1
$loop->parent->first: 1
$loop->parent->last: 1
$loop->parent->even:
$loop->parent->odd: 1
$loop->parent->depth: 1