laravel blades

Blade Templates

Blade is the simple, yet powerful templating engine provided with 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.

Template Inheritance

Two of the primary benefits of using Blade are template inheritance and sections. Since most web applications maintain the same general layout across various pages, it's convenient to define this layout as a single Blade view:
<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>
As you can see, this file contains typical HTML mark-up. However, take note of the @section and @yield directives. The @section directive defines a section of content, while the @yield directive is used to display the contents of a given section.

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.
@extends('layouts.app')

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

@section('sidebar')
    @parent

    

This is appended to the master sidebar.

@endsection @section('content')

This is my body content.

@endsection
In this example, the sidebar section is utilizing the @parent directive to append content to the layout's sidebar. The @parent directive will be replaced by the content of the layout when the view is rendered.

Displaying Data

You may display data passed to your Blade views by wrapping the variable in curly braces.
Welcome, {{ $username }}
You may also echo the results of any PHP function.
{{ time() }}

Echoing Data If Exists

Sometimes you may wish to echo a variable, but you aren't sure if the variable has been set. Instead of writing a ternary statement, Blade provides you with the following convenient shortcut which will be compiled to the ternary statement.
Welcome, {{ $username or 'Default' }}

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:
Welome, {!! $username !!}

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($data) === 1)
    I have one data!
@elseif (count($data) > 1)
    I have multiple data!
@else
    I don't have any data!
@endif
For convenience, Blade also provides an @unless directive:
@unless (Auth::check())
    You are not signed in.
@endunless

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)
    This is user {{ $user->id }}
@endforeach

@forelse ($users as $user)
    {{ $user->name }}
@empty
    No users
@endforelse

@while (true)
    I'm looping forever.
@endwhile

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 --}}

PHP TAG

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

Including Sub-Views

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:
@include('layouts.navigation')

Pluralization

Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization. By using a "pipe" character, you may distinguish singular and plural forms of a string:
'apples' => 'There is one apple|There are many apples',
echo trans_choice('messages.apples', 10);

0 Like 0 Dislike 0 Comment Share

Leave a comment