This article contains 4th articles in the Laravel5 framework learning series, which will briefly introduce Blade. why should we introduce it? laravel's template engine uses the blade template engine, so .... multiple pages may contain the same content, such as file headers, linked css or js. We can use the layout file to complete this function.
Let's create a new layout file, such as views/layout. blade. php.
Document
@yield('content')
We have created an uncertain structure and introduced bootstrap. Note that @ yield is the placeholder for the blade layout. in the future, our page content will be filled here and we will modify about. blade. php.
@extends('layout')@section('content') About {{ $first }} {{ $last }}@stop
The above code indicates that we use the layout file layout. blade. php and then add the content in the content section.
Add the following in routes. php:
Route::get('about', 'PagesController@about');Route::get('contact', 'PagesController@contact');
Add the following in PagesController. php:
public function contact() { return view('pages.contact'); }
Create view pages/contact. blade. php
@extends('layout')@section('content') Contact Me!@stop
Check it out!
You can add multiple @ yield in the layout file. for example, add @ yield ('footer ') in layout. blade. php '):
Document
@yield('content')
@yield('footer')
For example, you can put a script in contact. blade. php.
@extends('layout')@section('content') Contact Me!@stop@section('footer') 《script》 alert('Contact from scritp') 《script》@stop
A dialog box is displayed when you access contact, while about is still displayed normally.
Use @ if to determine
@extends('layout')@section('content') @if ($first = 'Zhang') Hello, Zhang @else Hello, nobody @endif@stop
It can also be seen as @ unless equivalent to if !, And @ foreach.
public function about() { $people = [ 'zhang san', 'li si', 'wang wu' ]; return view('pages.about', compact('people')); }@extends('layout')@section('content') Person:
@foreach($people as $person)
- {{ $person }}
@endforeach
@stop
In one case, the data may come from the database, and the set may be empty, such:
The code is as follows:
$ People = [];
To handle this situation, add @ if
@extends('layout')@section('content') @if (count($people)) Person:
@foreach($people as $person)
- {{ $person }}
@endforeach
@endif Other info@stop
That's better.
The above is all the content of this article, hoping to help you learn laravel5.