codovel

We no longer support this blog. Please visit this site instead. https://laravelprojectlab.com

Saturday, June 18, 2016

Laravel Layout and blade template introduction

For example, you create 2 veiws (main.blade.php and view1.blade.php) and its content are as below

main.blade.php
<html>
<head>
    <title>Laravel</title>
<body>
<h1>Header</h1>
@yield('content')
<h4>Footer</h4>
</body>
</html>

view1.blade.php
@extends('main')
@section('content')
    Here is my body
@endsection

Please check the result below.
If you render main.blade.php, you will see like this

And if you render view1.blade.php, you will see like this


Based on the code and result. Let me explain how blade engine work.
 

@yield('content') is used in a main or master view. you can put @yield code at any part of your view. Putting this line of code mean that this section will be replaced by other view.

And how to use the layout file (main.blade.php)  in normal view (view1.blade.php)

@extends('main') putting this code in view1.blade.php file mean that you want to use view main.blade.php and replace some section in side the file.

@section('content')...@endsection it mean that you want to replace the @yield('content') section in main.blade.php with the code inside this section block.

  • Blade Template 
blade template in Laravel is used to remove php block <?php ?>. It mean that normally if you want to write
php code in view file, you have to write it in this block <?php ?>. But in blade template, you can use @ instead.

Ex:
{{ $var }}    =>   <?php echo htmlentities($var);?>  
{!! $var  !!}   =>  <?php echo $var;?>
@if ....@endif    => <? if(){....}  ?>

I hope you can understand because it is very basic.

No comments: