Blade is a templating engine that powers simple PHP logic inside your HTML files. Blade is primarily used to include templates and data inside your files, whilst allowing you to use loops and conditions to show content.
<!-- Stored in resources/views/child.blade.php -->
@extends('layouts.app')
@section('title', 'Page Title')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
@extends('directory.file')
This inherits a specific layout. This is used at the top of every view that should use this layout. Typically, blade templates are stored in /resources/views/layouts/
and are named template.blade.php
, where template is the name of your blade layout. The primary blade template is traditionally named app.blade.php
.
@section('Name', 'Content')
@section('Name')
Content
@endsection
Define a section of content for a view. This is generally used in a view that uses a blade template.
@parent
can be used to append the content to the layout, rather than overwriting it. The @parent
directive will be replaced by the content of the layout when the view is rendered.
@yield('Name', Default_Value)
Display the contents of a given section. This is generally used in a blade template to display content specified by a view. You can automatically yield a section using the @show directive instead of @endsection.
@section('Name')
Content
@show
Components and Slots add the ability to create static modules with dynamic information inside of them. Not all of the information has to be dynamic, which is what makes this concept more popular than section directives.
Example of a component:
<!-- /resources/views/alert.blade.php -->
<div class="alert alert-danger">
</div>
The component in its entirety is just a div
tag associated with a couple of Bootstrap classes. To make it more dynamic, you can add a slot.
<div class="alert alert-danger">
{{ $slot }}
</div>
The $slot
variable will contain the content we wish to inject into the component.
To construct the component:
@component('alert')
<strong>Whoops!</strong> Something went wrong!
@endcomponent
The content inside the of the component would be defaulted to the $slot variable.
Sometimes it is helpful to define multiple slots for a component. To add more slots, simply add more variables.
<!-- /resources/views/alert.blade.php -->
<div class="alert alert-danger">
<div class="alert-title">{{ $title }}</div>
{{ $slot }}
</div>
The main difference now is the ability to differentiate them within the component call. This can be done using the @slot
directive.
@component('alert')
@slot('title')
Forbidden
@endslot
You are not allowed to access this resource!
@endcomponent
Any content not within a @slot
directive will be passed to the component in the $slot
variable.
@include('directory.file')
This will include all of the content from directory.file into your current view.
If the sub-view has variables that you wish to value, you can add those values as an associative array in the second parameter.
@include('view.name', ['some' => 'data'])
If the view may or may not exist:
@includeIf('view.name', ['some' => 'data'])
If the view should be included based on a Boolean condition:
@includeWhen($boolean, 'view.name', ['some' => 'data'])
To include the first view that exists from a given array of views:
@includeFirst(['custom.admin', 'admin'], ['some' => 'data'])
If, Elseif, Else:
@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 (Opposite of if):
@unless (Auth::check())
You are not signed in.
@endunless
Isset and Empty:
@isset($records)
// $records is defined and is not null...
@endisset
@empty($records)
// $records is "empty"...
@endempty
If the user is Authenticated (Logged In):
@auth('guard')
// The user is authenticated...
@endauth
@guest('guard')
// The user is not authenticated...
@endguest
**@switch($i)
@case(1)
First case...
@break
@case(2)
Second case...
@break
@default
Default case...
@endswitch**
@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
To 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