Home > Backend Development > PHP Tutorial > Laravel 5 Basics (4) - Introduction to Blade

Laravel 5 Basics (4) - Introduction to Blade

WBOY
Release: 2016-08-08 09:26:55
Original
1018 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

Add in PagesController.php:

<code>    public function contact() {
        return view('pages.contact');
    }</code>
Copy after login

New view pages/contact.blade.php

<code>@extends('layout')

@section('content')
    <h1>Contact Me!</h1>
@stop</code>
Copy after login

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>
Copy after login

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>
Copy after login

There will be a dialog box when accessing contact, but about is still displayed normally

  • Use @if to judge
<code>@extends('layout')

@section('content')
    @if ($first = 'Zhang')
        <h1>Hello, Zhang</h1>
    @else
        <h1>Hello, nobody</h1>
    @endif
@stop</code>
Copy after login

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>
Copy after login
<code>@extends('layout')

@section('content')
    <h1>Person:</h1>
    <ul>
        @foreach($people as $person)
            <li>{{ $person }}</li>
        @endforeach
    </ul>
@stop</code>
Copy after login

There is a situation where the data may come from the database and the collection may be empty, like this:

<code>$people = [];</code>
Copy after login

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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template