Readouble

Laravel 4.2 テンプレート

コントローラーレイアウトController Layouts

Laravelのテンプレートを使用する一つの方法は、コントローラーのレイアウトで指定する方法です。layoutプロパティーをコントローラーで指定すれば、指定されたビューが生成され、アクションからレスポンスが自動的にリターンされます。One method of using templates in Laravel is via controller layouts. By specifying the layout property on the controller, the view specified will be created for you and will be the assumed response that should be returned from actions.

コントローラーでレイアウトを宣言するDefining A Layout On A Controller

class UserController extends BaseController {

	/**
	 * レスポンスで必ず使用するレイアウト
	 */
	protected $layout = 'layouts.master';

	/**
	 * ユーザープロファイル表示
	 */
	public function showProfile()
	{
		$this->layout->content = View::make('user.profile');
	}

}

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

<!-- app/views/layouts/master.blade.phpとして保存 -->

<html>
	<body>
		@section('sidebar')
			ここは親のサイドバー
		@show

		<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 : 'Default' }}}

三項演算子を書く代わりに、Bladeでは次のような短縮形を使用することができます。However, instead of writing a ternary statement, Blade allows you to use the following convenient short-cut:

{{{ $name or 'Default' }}}

波括弧をそのまま出力する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:

@{{ ここはBladeで処理されません }}

もちろん、ユーザーから提示されたデータはエスケープするか、クリーンアップしましょう。出力をエスケープしたい場合は、三重の波括弧を使用してください。Of course, all user supplied data should be escaped or purified. To escape the output, you may use the triple curly brace syntax:

こんにちは、{{{ $name }}}.

データーをエスケープしたくない場合は、二重の波括弧を使用してください。If you don't want the data to be escaped, you may use double curly-braces:

こんにちは、{{ $name }}.

**注意:**アプリケーションのユーザーから提供されたコンテンツを出力する時は注意を払ってください。三重の波括弧を使用すると、コンテンツ中のHTMLエンティティーは、いつでもエスケープされます。Note: Be very careful when echoing content that is supplied by users of your application. Always use the triple curly brace syntax to escape any HTML entities in the content.

If文If Statements

@if (count($records) === 1)
	I have one record!
@elseif (count($records) > 1)
	I have multiple records!
@else
	レコードがなかった!
@endif

@unless (Auth::check())
	あなたはログインしていません。
@endunless

ループLoops

@for ($i = 0; $i < 10; $i  )
	The current value is {{ $i }}
@endfor

@foreach ($users as $user)
	<p>これはユーザーID: {{ $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', array('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)

コメント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コンパイラには独自ディレクティブを定義するために必要なコードの生成を行う、createMatchercreatePlainMatcherヘルパメソッドが用意されています。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);
});

章選択

Artisan CLI

設定

明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

Pagination和文
ペジネーション
ペギネーション
ページネーション
ページ付け
Scaffold和文
スカフォールド
スキャフォールド
型枠生成
本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作