Версия фреймворка: 8.x 5.4 4.2 4.0


Controller Layouts

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 {

	 * The layout that should be used for responses.
	protected $layout = 'layouts.master';

	 * Show the user profile.
	public function showProfile()
		$this->layout->content = View::make('user.profile');


Blade Templating

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.

Defining A Blade Layout

<!-- Stored in app/views/layouts/master.blade.php -->

			This is the master sidebar.

		<div class="container">

Using A Blade Layout



	<p>This is appended to the master sidebar.</p>

	<p>This is my body content.</p>

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.

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');

Other Blade Control Structures

Echoing Data

Hello, {{ $name }}.

The current UNIX timestamp is {{ time() }}.

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:

Displaying Raw Text With Curly Braces

@{{ This will not be processed by Blade }}

Of course, all user supplied data should be escaped or purified. To escape the output, you may use the triple curly brace syntax:

Hello, {{{ $name }}}.
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 Statements

@if (count($records) === 1)
	I have one record!
@elseif (count($records) > 1)
	I have multiple records!
	I don't have any records!

@unless (Auth::check())
	You are not signed in.


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

@foreach ($users as $user)
	<p>This is user {{ $user->id }}</p>

@while (true)
	<p>I'm looping forever.</p>

Including Sub-Views


You may also pass an array of data to the included view:

@include('view.name', array('some'=>'data'))

Overwriting Sections

By default, sections are appended to any previous content that exists in the section. To overwrite a section entirely, you may use the overwrite statement:


	<p>This is an item of type {{ $item->type }}</p>

Displaying Language Lines


@choice('language.line', 1);


{{-- This comment will not be in the rendered HTML --}}