Blade Templating

A Blade Layout

<!-- Stored in resources/views/layouts/master.blade.php -->
<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>

Using A Blade Layout

@extends('layouts.master')

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

@section('sidebar')
    @parent

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

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

Yield Default value

@yield('section', 'Default Content')

Echoing Data

{{ $name }}.

// Echo escaped content
 {{{ $var }}}

Echoing Data After Checking For Existence

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

Displaying Raw Text With Curly Braces

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

Control Structure

If Statements

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

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

Loops

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

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

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

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

Including Sub-Views

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

Comments

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

results matching ""

    No results matching ""