Laravel Blade Template Engine

Laravel Blade Template Engine

Laravel Blade Template Engine

Laravel provide simple and very powerful templating system. This templating engine combines one or more templates which is driven by inheritance and section to produce resulting views. Blade have its own controlling structure such as loops and conditional statements.Inheritance and Section are two primary benefits of using Blade template engine.To create Blade template, simply create your view file with the extension .blade.php instead of .php and it is typically stored in the resources/views directory.

Example of Simple Blade Layout :

  1. <!-- Stored in resources/views/layouts/default.blade.php -->
  2. <html>
  3. <head>
  4. <title>Application Name - @yield('title')</title>
  5. </head>
  6. <body>
  7. @section('sidebar')
  8. This is the default sidebar.
  9. @show
  10. <div class="container">
  11. @yield('content')
  12. </div>
  13. </body>
  14. </html>

As you can notice that file contains HTML tags follow with some blade sytax which is known as @section and @yield .The @section directive define a section of content while the @yield define the content area, In this section conent of pages will display.

Now let's create a child page which inherits this default or master layout.

Extending or Inheriting a Layout

To inherit master layout in child pages, you will have to use @extends directive.You can also inject content in your section from child page by using @section directive.


  1. <!-- Stored in resources/views/child.blade.php -->
  2. @extends('layouts.default')
  3. @section('title', 'Page Title')
  4. @section('sidebar')
  5. <p>This is appended to the default sidebar.</p>
  6. @endsection
  7. @section('content')
  8. <p>Define body content area.</p>
  9. @endsection
Echoing Data

You can display your data by using `curly` braces. For example :

Welcome {{ $name }}

Checking data if exist

If you don't know variable is set or not then you use ternary operator in PHP, in same way you will use in Laravel.

{{ isset($name) ? $name : 'Default text' }} 

Instead of writing a ternary statement, Blade provide a short-cut

{{ $name or 'Default text' }}
Control Structures

Blade provide short-cuts for common PHP control structures.

IF Statements

You will use if-else statements through @if @elseif @else @endif directives.


  1. @if (count($data) === 1)
  2. I have one data!
  3. @elseif (count($data) > 1)
  4. I have multiple data!
  5. @else
  6. I don't have any data!
  7. @endif
Loop :

  1. @for ($i = 0; $i < 10; $i++)
  2. The current value is {{ $i }}
  3. @endfor
  4. @foreach ($results as $result)
  5. <p>This is result of {{ $result->id }}</p>
  6. @endforeach
  7. @while (true)
  8. <p>I'm looping forever.</p>
  9. @endwhile
Including Files

Blade provide @include directive that allow you to include file easily.


  1. <div>
  2. <form>
  3. @include('foldername.filename')
  4. </form>
  5. </div>

Phone: (+91) 8800417876
Noida, 201301
sakarya escort akyazı escort arifiye escort erenler escort eve gelen escort ferizli escort geyve escort hendek escort otele gelen escort sapanca escort söğütlü escort taraklı escort
sakarya escort | sakarya escort | adapazarı escort | sakarya escort | sakarya escort | sapanca escort | sakarya escort | sakarya escort | serdivan escort | sakarya escort | karasu escort | sakarya escort | sakarya escort | sapanca escort | sakarya escort | sakarya escort | sakarya escort | sapanca escort | sakarya escort | hendek escort bayan sakarya escort adresleri.