Introduction to Blade for Laravel5 framework learning

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.