BladeテンプレートBlade Templating
Bladeはシンプルですが、パワフルなLaravelのテンプレートエンジンです。コントローラーレイアウトと違い、Bladeはテンプレートの継承とセクションにより扱われます。全てのBladeテンプレートには.blade.php拡張子が付きます。Blade is a simple, yet powerful templating engine provided with Laravel. Unlike controller layouts, Blade is driven by template inheritance and sections. All Blade templates should use the .blade.php extension.
Bladeレイアウトを定義するDefining A Blade Layout
<!-- Stored in resources/views/layouts/master.blade.php -->
<html>
	<body>
		@section('sidebar')
			ここにマスターサイドバー
		@stop
		<div class="container">
			@yield('content')
		</div>
	</body>
</html>
Bladeレイアウトを使用するUsing A Blade Layout
@extends('layouts.master')
@section('sidebar')
	@parent
	<p>ここはマスターサイドバーに追加される</p>
@stop
@section('content')
	<p>ここはコンテンツの本文</p>
@stop
Bladeレイアウトを「拡張」するには、ただレイアウトのセクションをオーバーライドするだけです。子のビューで親のコンテンツをインクルードするには@parentディレクティブをセクションで使用します。サイドバーやフッターでレイアウトのコンテンツを追加する場合などに便利です。Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent directive in a section, allowing you to append to the contents of a layout section such as a sidebar or footer.
セクションを定義するべきか迷うこともあります。そんな場合は、デフォルト値を@yieldに直接指定できます。2つめのパラメーターで、デフォルト値を指定してください。Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the @yield directive. You may pass the default value as the second argument:
@yield('section', 'Default Content')
その他のBlade制御構文Other Blade Control Structures
データーをechoするEchoing Data
こんにちは、{{ $name }}さん。
現在のUnixタイムスタンプは、{{ time() }}です。
存在を確認した後にデーターをechoするEchoing Data After Checking For Existence
時々、セットされてるかわからない変数をechoしたい場合があります。基本的に、このように実現できます。Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Basically, you want to do this:
{{ isset($name) ? $name : 'デフォルト値' }}
三項演算子を書く代わりに、Bladeでは次のような短縮形を使用することができます。However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:
{{ $name or 'デフォルト値' }}}
波括弧をそのまま出力するDisplaying Raw Text With Curly Braces
カギカッコで囲まれた文字列をそのまま出力する必要がある場合には、@を先頭に付けることで、Bladeの処理をエスケープすることができます。If you need to display a string that is wrapped in curly braces, you may escape the Blade behavior by prefixing your text with an @ symbol:
@{{ This will not be processed by Blade }}
データをエスケープしたくない場合は、以下のような記述をしてください。If you don't want the data to be escaped, you may use the following syntax:
こんにちは、{!! $name !!}さん。
注意: アプリケーションのユーザーから入力された内容を表示する時には注意が必要です。内容をHTMLエンティティへエスケープするために、二重の波括弧記法をいつも使用すべきでしょう。Note: Be very careful when echoing content that is supplied by users of your application. Always use the double curly brace syntax to escape any HTML entities in the content.
If文If Statements
@if (count($records) === 1)
	レコードは一つ!
@elseif (count($records) > 1)
	複数のレコードがある!
@else
	レコードはない!
@endif
@unless (Auth::check())
	あなたはログインしていない!
@endunless
ループLoops
@for ($i = 0; $i < 10; $i++)
	現在の値は、{{ $i }}です。
@endfor
@foreach ($users as $user)
	<p>ユーザー{{ $user->id }}です。</p>
@endforeach
@forelse($users as $user)
  	<li>{{ $user->name }}</li>
@empty
  	<p>ユーザーなし</p>
@endforelse
@while (true)
	<p>永久にループ中</p>
@endwhile
サブビューをインクルードするIncluding Sub-Views
@include('view.name')
読み込むビューにデーターの配列を渡すことも可能です。You may also pass an array of data to the included view:
@include('view.name', ['some' => 'data'])
セクションのオーバーライドOverwriting Sections
セクション全体を書き換えるには、overwrite文を使用してください。To overwrite a section entirely, you may use the overwrite statement:
@extends('list.item.container')
@section('list.item.content')
	<p>これは、{{ $item->type }}タイプのアイテムです。</p>
@overwrite
多言語に対応済みの行表示Displaying Language Lines
@lang('language.line')
@choice('language.line', 1)
@choice('language.line', 1);
コメントComments
{{-- このコメントは、HTMLにレンダーされない --}}
Bladeの拡張Extending Blade
Bladeでは、皆さん独自のカスタム記述法を定義することもできます。Bladeファイルがコンパイルされるとき、各カスタム記法定義がビューの内容で呼び出され、簡単なstr_replaceでの置き換えから、複雑な正規表現による置換まで、実行することができます。Blade even allows you to define your own custom control structures. When a Blade file is compiled, each custom extension is called with the view contents, allowing you to do anything from simple str_replace manipulations to more complex regular expressions.
Bladeコンパイラには独自ディレクティブを定義するために必要なコードの生成を行う、createMatcherとcreatePlainMatcherヘルパメソッドが用意されています。The Blade compiler comes with the helper methods createMatcher and createPlainMatcher, which generate the expression you need to build your own custom directives.
createPlainMatcherメソッドは引数を取らない、@endifや@stopのようなディレクティブに使用し、一方のcreateMatcherは引数と一緒に使用します。The createPlainMatcher method is used for directives with no arguments like @endif and @stop, while createMatcher is used for directives with arguments.
@datetime($var)により、シンプルに$varを->format()で呼び出すディレクティブの例を以下で紹介します。The following example creates a @datetime($var) directive which simply calls ->format() on $var:
Blade::extend(function($view, $compiler)
{
	$pattern = $compiler->createMatcher('datetime');
	return preg_replace($pattern, '$1<?php echo $2->format(\'m/d/Y H:i\'); ?>', $view);
});