Laravel のディレクティブ一覧(を調べる方法)
はじめに
Laravel には @if や @csrf など、Blade ファイル中で利用可能な便利なディレクティブが用意されている。
「利用可能なディレクティブとして何があるかをソースから調べる方法」を整理した。
※Bladeテンプレート - Readouble にも記載はあるが、一覧として見たかった。
なお、あくまでも「何があるか」なので各ディレクティブの使用方法などは特に書いてない。
TL;DR
Illuminate/View/Compilers/Concerns/を調べるgrep,awk,sedあたりを使うと OK
目次
環境・条件
1 | $ composer info laravel/framework |
詳細
結論: Laravel(v5.7.28) で利用可能なディレクティブ
以下が Laravel v5.7.28 における全ディレクティブ(のはず)。
@can@cannot@canany@elsecan@elsecannot@elsecanany@endcan@endcannot@endcanany@comments@component@endcomponent@slot@endslot@auth@elseauth@endauth@guest@elseguest@endguest@hassection@if@unless@elseif@else@endif@endunless@isset@endisset@switch@case@default@endswitch@echos@rawechos@regularechos@escapedechos@csrf@dd@dump@method@each@include@includeif@includewhen@includefirst@inject@json@extends@section@parent@yield@show@append@overwrite@stop@endsection@forelse@empty@endforelse@endempty@for@foreach@break@continue@endfor@endforeach@while@endwhile@php@unset@stack@push@endpush@prepend@endprepend@lang@endlang@choice
抽出方法
where do I get all Blade built-in directives? に書かれている通り、Illuminate/View/Compilers/BladeCompiler.php および Illuminate/View/Compilers/Concerns/CompilesXXXX.php でディレクティブは定義されている。
vendor/laravel/framework/src/Illuminate/View/Compilers/BladeCompiler.php で、どのファイルを読み込むかが記述させている。
1 |
|
で、各ディレクティブの内容自体は Illuminate/View/Compilers/Concerns/CompilesXXXX.php に定義。
vendor/laravel/framework/src/Illuminate/View/Compilers/Concerns/CompilesLoops.php の一部を抜粋。
@for (xxx) は ` として変換、出力するようになっている。
1 |
|
というわけで Illuminate/View/Compilers/Concerns/ の compileXxxx を抽出すれば、それが利用可能なディレクティブということになる(はず)。
grep, awk, sed で抽出。
※OS によってコマンドの挙動やオプションなどの違いにより下記コマンドそのままだと上手くいかないかも。
1 | $ \grep -i 'function compile' vendor/laravel/framework/src/Illuminate/View/Compilers/Concerns/* \ |
抽出結果は前の項で書いた通り。
まとめ
Illuminate/View/Compilers/Concerns/を調べるgrep,awk,sedあたりを使うと OK
参考文献
関連記事
- Laravel で現在の URL 取得方法まとめ
- Laravel で Cookie を使う(参照/設定/削除)
- Laravel で PDF ファイルをブラウザで開く
- Laravel で withCount と having を使って絞り込み
- Laravel で「開始日時 < 終了日時」であることをバリデーションする
- Laravel で生 SQL を実行
- JavaScript で URL のクエリパラメータを操作する方法
- jQuery Select2 で、初期値の設定と選択状態のクリア