Blade Templates

Lightweight and Simplified kind of Laravel's Blade Template

[Since 0.5.0]

The Pletfix ViewCompiler based on Laravel's Blade Compiler and therefore this manual based on the original Documentation of Laravel's Blade Templates. It's open-sourced software licensed under the MIT license.

Thank's Taylor Otwell for his grateful work!

Introduction

Pletfix provides a simplified kind of Blade Template, a powerful templating engine by Laravel.

Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.

Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.

The views will be compiled automatically on the first rendering. The compiled views are cached in storage/cache/views directory.

Usage

Defining A Layout

Two of the primary benefits of using Blade are template inheritance and sections. To get started, let's take a look at a simple example. First, we will examine a "master" page layout. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:

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

<html>
    <head>
        <title>@yield('title')</title>
    </head>
    <body
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

As you can see, this file contains typical HTML mark-up. However, take note of the @yield directives, it is a kind of placeholder, which content is defined in the child pages.

Now that we have defined a layout for our application, let's define a child page that inherits the layout.

Extending A Layout

When defining a child view, use the Blade @extends directive to specify which layout the child view should "inherit". Views which extend a Blade layout may inject content into the layout's sections using @section directives. Remember, as seen in the example above, the contents of these sections will be displayed in the layout using @yield:

<!-- Stored in resources/views/child.blade.php -->

@extends('layouts.app')

@section('title', 'Page Title')

@section('content')
    <p>This is my body content.</p>
@endsection

Blade views may be returned from routes using the global view helper:

Route::get('blade', function () {
    return view('child');
});

Including Partials

Blade's @include directive allows you to include a Blade view from within another view. All variables that are available to the parent view will be made available to the included view:

<div>
    @include('shared.errors')

    <form>
        <!-- Form Contents -->
    </form>
</div>

Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:

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

You should avoid using the __DIR__ and __FILE__ constants in your Blade views, since they will refer to the location of the compiled view.

Displaying Data

You may display data passed to your Blade views by wrapping the variable in curly braces. For example, given the following route:

Route::get('greeting', function () {
    return view('welcome', ['name' => 'Samantha']);
});

You may display the contents of the name variable like so:

Hello, {{ $name }}.

Of course, you are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:

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

Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks.

Echoing Data If It Exists

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. We can express this in verbose PHP code like so:

{{ isset($name) ? $name : 'Default' }}

However, instead of writing a ternary statement, Blade provides you with the following convenient shortcut, which will be compiled to the ternary statement above:

{{ $name or 'Default' }}

In this example, if the $name variable exists, its value will be displayed. However, if it does not exist, the word Default will be displayed.

Displaying Unescaped Data

By default, Blade {{ }} statements are automatically sent through PHP's htmlentities function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:

Hello, {!! $name !!}.

Be very careful when echoing content that is supplied by users of your application. Always use the escaped, double curly brace syntax to prevent XSS attacks when displaying user supplied data.

Escape Blade Directive

Since many JavaScript frameworks also use "curly" braces to indicate a given expression should be displayed in the browser, you may use the @ symbol to inform the Blade rendering engine an expression should remain untouched. For example:

<h1>Laravel</h1>

Hello, @{{ name }}.

In this example, the @ symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework.

Control Structures

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures, while also remaining familiar to their PHP counterparts.

If Statements

You may construct if statements using the @if, @elseif, @else, and @endif directives. These directives function identically to their PHP counterparts:

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

Loops

In addition to conditional statements, Blade provides simple directives for working with PHP's loop structures. Again, each of these directives functions identically to their PHP counterparts:

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

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

@while (true)
    <p>I am looping forever.</p>
@endwhile

When using loops you may also end the loop or skip the current iteration:

@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif

    <li>{{ $user->name }}</li>

    @if ($user->number == 5)
        @break
    @endif
@endforeach

You may also include the condition with the directive declaration in one line:

@foreach ($users as $user)
    @continue($user->type == 1)

    <li>{{ $user->name }}</li>

    @break($user->number == 5)
@endforeach

Embedded PHP

In some situations, it's useful to embed PHP code into your views. You can use the Blade @php directive to execute a block of plain PHP within your template:

@php
    //
@endphp

While Blade provides this feature, using it frequently may be a signal that you have too much logic embedded within your template.

Comments

Blade also allows you to define comments in your views. However, unlike HTML comments, Blade comments are not included in the HTML returned by your application:

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

Extending Blade

Not implemented yet, planned in future.

Quick Reference

Displaying Data

{{ $var }}                      - Echo escaped content
{!! $var !!}                    - Echo raw content
{{-- Comment --}}               - A Blade comment
{{ $var or 'default' }}         - Echo escaped content with a default value
{!! $var or 'default' !!}       - Echo raw content with a default value

Masking

@{{ $var }}                     - Initiates HTML code ("@" will be skipped)
@{!! $var !!}                   - Initiates HTML code ("@" will be skipped)
@@anyexpression                 - Initiates HTML code (the first "@" will be skipped)

Extending a Layout

@extends('layout')              - Extends a template with a layout

Sections

@yield('section')               - Yields content of a section
@yield('section', 'default')    - Yields content of a section with a default value
@section('name', 'content')     - Section
@section('name')                - Starts a section block
@endsection                     - Ends section block 

Including Partials

@include('view')                - Includes a partial view
@include('view', $vars)         - Includes a partial view with additional variables.

If Statements

@if(condition)                  - Starts an if block
@else                           - Starts an else block
@elseif(condition)              - Start an elseif block
@endif                          - Ends an if block

Loops

@for($i = 0; $i < 10; $i++)     - Starts a for block
@endfor                         - Ends a for block
@foreach($list as $val)         - Starts a foreach block
@foreach($list as $key => $val) - Starts a foreach block with key
@endforeach                     - Ends a foreach block
@while(condition)               - Starts a while block
@endwhile                       - Ends a while block
@continue                       - Skip the current iteration (allowed only within a loop)
@continue(condition)            - Skip the current iteration if the condition is true (allowed only within a loop)
@break                          - Break the current iteration (allowed only within a loop)
@break(condition)               - Break the current iteration if the condition is true (allowed only within a loop)

User Role Check

@is($role)                      - Starts a block if the current user have the given role
@elseis($role)                  - Starts an else block if the current user have the given role
@elseis                         - Starts an else block
@endis                          - Ends an the block
@isnot($role)                   - Starts a block if the current user have not the given role
@elseisnot($role)               - Starts an else block if the current user have not the given role
@elseisnot                      - Starts an else block
@endisnot                       - Ends an is-not block

User Ability Check

@can($ability)                  - Starts a block if the current user have the given ability
@elsecan($ability)              - Starts an else block if the current user have the given ability
@elsecan                        - Starts an else block
@endcan                         - Ends the block
@cannot($ability)               - Starts a block if the current user have not the given ability
@elsecannot($ability)           - Starts an else block if the current user have not the given ability
@elsecannot                     - Starts an else block
@endcannot                      - Ends the block

Embedded PHP

@php(phpcode)                   - PHP code
@php                            - Starts a block of PHP code
@endphp                         - Ends PHP code

Not supported Laravel's Blade Commands 5.3

@each (Rendering Collections)
@forelse, @endforelse, @empty
@includeIf
@inject (Service Injection)
$lang
$loop (Loop variables)
@parent
@push, @endpush (Stacks)
@unless, @endunless
@verbatim, @endverbatim
Extending Blade (planned in future)

(edit on GitHub)