Laravel Blade Template Engine – onlinecode

Laravel Blade Template Engine – onlinecode

In this post we will give you information about Laravel Blade Template Engine – onlinecode. Hear we will give you detail about Laravel Blade Template Engine – onlinecodeAnd how to use it also give you demo for it if it is necessary.

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. <divclass="container">
  11. @yield('content')
  12. </div>
  13. </body>
  14. </html>
<!-- Stored in resources/views/layouts/default.blade.php --><html>    <head>        <title>Application Name - @yield('title')</title>    </head>    <body>        @section('sidebar')            This is the default sidebar.        @show        <div >            @yield('content')        </div>    </body></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
<!-- Stored in resources/views/child.blade.php -->@extends('layouts.default')@section('title', 'Page Title')@section('sidebar')        <p>This is appended to the default sidebar.</p>@endsection@section('content')    <p>Define body content area.</p>@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
@if (count($data) === 1)    I have one data!@elseif (count($data) > 1)    I have multiple data!@else    I don't have any data!@endif

Loop :

  1. @for($i=;$i<10;$i++)
  2. The current value is {{$i}}
  3. @endfor
  4. @foreach($resultsas$result)
  5. <p>This is result of {{$result->id }}</p>
  6. @endforeach
  7. @while(true)
  8. <p>I'm looping forever.</p>
  9. @endwhile
@for ($i = 0; $i < 10; $i++)    The current value is {{ $i }}@endfor@foreach ($results as $result)    <p>This is result of  {{ $result->id }}</p>@endforeach@while (true)    <p>I'm looping forever.</p>@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>
<div>    <form>         @include('foldername.filename')        </form></div>
Now you can use Blade features in your laravel application. Click here to see the Laravel 5.2 CRUD Example –
Laravel 5.2 CRUD (Create Read Update Delete) Example from Scratch

Label :


Laravel PHP Framework

Web Development

Hope this code and post will helped you for implement Laravel Blade Template Engine – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

11  +    =  14

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US