Home >Backend Development >PHP Tutorial >Laravel 5 Basics (4) - Introduction to Blade
We may include the same content in multiple pages, such as file headers, linked css or js, etc. We can use layout files to accomplish this function.
Let’s create a new layout file, for example views/layout.blade.php
<code><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css"> </head> <body> <div class="container"> @yield('content') </div> </body> </html></code>
We created an incomprehensible structure and introduced bootstrap. Note that @yield
is the layout placeholder of blade. Our page content will be filled here in the future. Modify about.blade.php
<code>@extends('layout') @section('content') <h1>About {{ $first }} {{ $last }}</h1> @stop</code>
The above code means that we use the layout file layout.blade.php
, and then add content in the content
section.
Add in routes.php
:
<code>Route::get('about', 'PagesController@about'); Route::get('contact', 'PagesController@contact');</code>
Add in PagesController.php
:
<code> public function contact() { return view('pages.contact'); }</code>
New view pages/contact.blade.php
<code>@extends('layout') @section('content') <h1>Contact Me!</h1> @stop</code>
Check it out!
We can add multiple @yield
in the layout file, such as adding @yield('footer')
in layout.blade.php
:
<code><!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css"> </head> <body> <div class="container"> @yield('content') </div> @yield('footer') </body> </html></code>
For example, if there is a script in contact.blade.php
, it can be placed in this section.
<code>@extends('layout') @section('content') <h1>Contact Me!</h1> @stop @section('footer') <script> alert('Contact from scritp') </script> @stop</code>
There will be a dialog box when accessing contact, but about is still displayed normally
@if
to judge<code>@extends('layout') @section('content') @if ($first = 'Zhang') <h1>Hello, Zhang</h1> @else <h1>Hello, nobody</h1> @endif @stop</code>
can also be regarded as @unless
equivalent to if !
, and @foreach
etc.
<code> public function about() { $people = [ 'zhang san', 'li si', 'wang wu' ]; return view('pages.about', compact('people')); }</code>
<code>@extends('layout') @section('content') <h1>Person:</h1> <ul> @foreach($people as $person) <li>{{ $person }}</li> @endforeach </ul> @stop</code>
There is a situation where the data may come from the database and the collection may be empty, like this:
<code>$people = [];</code>
To handle this situation, please add @if
handle
<code>@extends('layout') @section('content') @if (count($people)) <h1>Person:</h1> <ul> @foreach($people as $person) <li>{{ $person }}</li> @endforeach </ul> @endif <h2>Other info</h2> @stop</code>
That's better.
The above introduces the basics of Laravel 5 (4) - Introduction to Blade, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.