Blade: Laravel inbuilt template engine

laravel_blad

Introduction

PHP is getting popularity due to easy to learn and easy to use. It can be easily understood code by looking at php file that have logic inside php tags and printing html based on data that php have. It is fine with embedded php inside html when there are few pages in web application. But if it is a latger application than it is necessary to extract logic in seperate php file and pass the data in another php file where php tags are used just for iteration, condition and printing data. We can achieve this by using template engine. There are numbers of template engines available in php but in larave you can find blade template engine which is powerful and easy to use template engine.

A template engine is nothing but provide facility to write short syntax in our php file where all presentation logic recides. It also provide to decalre common layout so that other pages can use it. You do not have to install blade template engine separately in you laravel project. It is already installed along with laravel. In this post we will briefly see it’s features in brief.

Example

If you have read my post Laravel MVC where i have created 2 views to display data served by controller where i have written all code using Please note that to take advantage of blade template we have to save our files using .blade extensions so you can see change in files names as below

  • product_list.blade.php
  • product_detail.blade.php

See older code which was written using <?php tags and echo,

Now lets use blade syntax to minimize <?php tag and echo also shorthand loop syntax.

Notice in above code in blade syntax **** will be replaced as {{}} and @foreach and @endforeach for **** and ****

Same way see Older product_detail.php

and how easily it is written using blade syntax.

More Feature

Layout:

Apart from above blade provides very useful concept to define layouts and reuse it in different views as per need. Let say i have to build a web-application where there are two sections.

  1. front-end: section where subscribers can post messages and interact with each other.
  2. Admin: Admin can manager all users and their subscriptions.

So i need to define two different layouts for front-end and admin so admin pages can extends admin_layout.blade.php and front pages can extends forntend_layout.blade.php.

Let me define admin_layout.blade.php

You can notice in above layout there are total 2 sections sidebar and content. Generally sidebar will be used for Navigation if you wish you can just put one section only as content and remove sidebar section and put navigation here but if you wish that some pages not required sidebar and some than please use 2 sections.

Now let’s define sidebar nav sidebar_nav.blade.php.

Now we include this file when ever we wish to add sidebar navigation menu in any page.
Let say it’s users.blade.php

Please note that in above page when ever any section start from layout there will be @section(‘section_name_from_layout’) written and at the end of that section @stop written.

I hope you understand this layout concepts which is almost same as ASP.NET master-page concept.

Partials:

Sometimes we need to write some reusable sections in one separate PHP file and includes in the pages when ever it required like common menu shared between all the pages is added as include_once or require once in PHP but here in blade you have to write @include(‘viewname’)

This is very helpful for building sections in your web-pages in display in any page like advertises, top-5 latest items and include it in the pages where ever you want.

There still more on laravel blade template sp please refer this link http://laravel.com/docs/4.2/templates#other-blade-control-structures

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s