Laravel 5 Basics (Iv.)-Blade Introduction

Source: Internet
Author: User
Tags php tutorial
In multiple pages we may contain the same content, such as the file header, linked CSS or JS. We can use the layout file to complete this function.

Let's create a new layout file, for exampleviews/layout.blade.php

    
  
       Document    
  
               @yield('content')    

We created a puzzled structure, introduced the bootstrap, note that the @yield layout placeholder for Blade, the future of our page content will be populated here, modifyabout.blade.php

@extends('layout')@section('content')    

About {{ $first }} {{ $last }}

@stop

The code above indicates that we use the layout file and layout.blade.php then content add content to the segment.

In the routes.php add:

Route::get('about', 'PagesController@about');Route::get('contact', 'PagesController@contact');

In the PagesController.php add:

    public function contact() {        return view('pages.contact');    }

New Viewpages/contact.blade.php

@extends('layout')@section('content')    

Contact Me!

@stop

Check it out!

In the layout file we can add multiple @yield , such as in the layout.blade.php add @yield('footer') :

    
  
       Document    
  
               @yield('content')        @yield('footer')

For example contact.blade.php , there is a script that can be placed in this section.

@extends('layout')@section('content')    

Contact Me!

@stop@section('footer') @stop

Access to the contact will have a dialog box, and about is still a normal display

    • Use @if to judge
@extends('layout')@section('content')    @if ($first = 'Zhang')        

Hello, Zhang

@else

Hello, nobody

@endif@stop

can also be @unless equated with if ! , and @foreach so on.

    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 might be from the database, and the collection might be empty, like this:

$people = [];

To handle this situation, add @if processing

@extends('layout')@section('content')    @if (count($people))        

Person:

    @foreach($people as $person)
  • {{ $person }}
  • @endforeach
@endif

Other info

@stop

That ' s better.

The above describes the Laravel 5 Basic (iv)-Blade introduction, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.