Blade Templates in Laravel

Recently I've been getting into Laravel and the templating engine being used for the View portion of the Laravel MVC is the awesome Blade templating engine. Generally speaking if you used one templating language you can easily figure out others.  At that point it mostly comes down to choosing a template language that suits us the most, for example, I find EJS syntax somewhat ugly,  while Blade syntax is very eye pleasing to le me.

Laravel - Blade Templates
Blade is called using @ symbol, to define a master layout we use something like this:
<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>

Templates use plain Html, but it needs to be wrapped into Blade directives, such as @section.

You can also extend layouts, use master templates (like for header and footer) in other templates:
@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
To return a temple view in a route we would just use the  global view helper in our Routes file:
Route::get('blade', function () {
    return view('child');
});

How Blade templates work

Hopefully you found this short tutorial interesting!

More detailed Blade tutorials coming soon!




Comments

Popular posts from this blog

10 Tips for Designing Better Test Cases

TestRigor - Review

How to Pass AZ-900 Azure Fundamentals Certification Exam