Laravel offers a custom templating engine called Blade.
Echoing Data
is functionally equivalent to
<?= htmlentities($variable) ?>
. If you want to echo without the escaping, use {!! and !!}.
Note: the difference between and
{!! $variable !!}
Let’s say we have a route
1 | Route::get('/', function () { |
And in your welcome blade view
1 | {{ $test }} // it will show <span>hello world</span> |
1 | // Parsed as Blade; the value of $bladeVariable is echoed to the view |
Control Structures
Conditionals
@if
Syntax
1 | @if(count($users) === 1) |
@unless
Syntax: @unless
condition is the same as <?php if(!$condition) ?>
1 | @unless(count($users) > 0) |
Loops
@for
1 | @for( $i = 0; $i < $users->slotsCount(); $i++ ) |
@foreach
1 | @foreach($users as $user) |
@while
1 | @while( $user = array_pop($users) ) |
@forelse
1 | @forelse($users as $user) |
$loop
within @foreach
and @forelse
Within a @foreach
or @forelse
loop, Laravel provides a $loop
variable, it will return a stdClass object with the following properties:
index
The 0-based index of the current item in the loop; 0 would mean ‘first item’
iteration
The 1-based index of the current item in the loop; 1 would mean ‘first itme’
remaining
How many items remain in the loop, if the current item is the first of three, this will be 2
count
The count of items in loop
first
A boolean indicating whether this is the first item in the loop
last
A boolean indicating whether this is the last item in the loop
depth
How many levels deep this loop is: 1 for a loop, 2 for a loop within a loop
parent
Refers to the
$loop
variable for the parent loop item; if this loop is within another@foreach
loop otherwise, null
1 | <ul> |
or
If you are not sure a variable is set or not, use or
can set a default value.
1 | {{$title or 'Default Title'}} // if $title is not set, 'Default title' will be echoed. |
Template Inheritance
Blade provides a structure for template inheritance that allows views to extend, modify, and include other views
Defining Sections with @section
/@show
and @yield
Let’s start with the layout
1 | <! resources/views/layouts/master.blade.php --> |
We defined 3 Blade directives, all three are defining that the section can be extended later. And all three are defining what to do if the section isn’t extended.
Now we can extend the master layout
1 | // We define that the view is extending master.blade.php |
We can either include the content from the parent by using the @parent
and then add something to it. Or, without @parent
, we can simply overwrite anything defined in the parent for this section by without section.
Note: the difference between @show
and @endsection
:
Use
@show
when you’re defining the place for a section, in the parent template. Use@endsection
when you’re defining the content for a template in a child template
@include
@include
allows us to pull a view into another view.
1 | <!-- resources/views/sign-up-button.blade.php --> |
1 | <!-- resources/views/home.blade.php --> |
@each
1 | <!-- resources/views/sidebar.blade.php --> |
Consider @each
syntax. The first parameter is the name of the view partial. The sctiond is the array or collection to iterate over. The thrid is the variable name that each item will be passed to the view as. And the optional fourth parameter is the view to show if the array or collection is empty.