Home  >  Article  >  PHP Framework  >  Laravel development: How to use Laravel Blade template layout?

Laravel development: How to use Laravel Blade template layout?

WBOY
WBOYOriginal
2023-06-14 10:41:191004browse

Laravel is an excellent development framework based on PHP. It has the advantages of being easy to learn, efficient, and safe, and is deeply loved by WEB developers. Among them, Laravel Blade template layout is a very practical function in the Laravel framework. This article will show you how to use Laravel Blade template layout through actual cases.

What is Blade template layout?

The Blade template engine is the default view engine of the Laravel framework. Compared with the template engine of PHP's native syntax, Blade supports a more concise and elegant syntax and can be used better with the Laravel framework. The Laravel Blade template layout refers to dividing the web page into a modular combination of header, tail, sidebar, and block content to facilitate separate development and improve development efficiency.

  1. Create the layout master template

In Laravel, we can use the artisan command to generate the layout master template. The specific steps are as follows:

php artisan make:layout master

After executing this command, a master template file named master.blade.php will be generated in the project resources/views/layouts/ directory. Open the file, and you can see that the code content is as follows:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>@yield('title')</title>
</head>
<body>
    <header>
        @yield('header')
    </header>
    <nav>
        @yield('nav')
    </nav>
    <main>
        @yield('content')
    </main>
    <footer>
        @yield('footer')
    </footer>
</body>
</html>

We can see that the template file contains different blocks such as header, tail, navigation bar, and body. Using Blade template syntax The @yield() function here defines a template block. We will use the @section() function in other view files to fill these template blocks.

  1. Replace the inherited subview

For any view file that needs to use layout, you can perform layout by inheriting the main template. Open the view file and add the following code:

@extends('layouts.master')

@extends('layouts.master') here represents the current view file Inherits the master template file layouts.master. Next, you can fill these template blocks with the template block names defined by the @yield() function. For example, you can add the following code to the view file:

@section('title', '页面标题')
@section('header')
    <h1>头部内容</h1>
@endsection
@section('nav')
    <ul>
        <li><a href="#">导航栏1</a></li>
        <li><a href="#">导航栏2</a></li>
        <li><a href="#">导航栏3</a></li>
    </ul>
@endsection
@section('content')
    <p>主体内容</p>
@endsection
@section('footer')
    <p>版权信息</p>
@endsection

In the above code, @section The () function is used to fill in the template section in the main template. For example, @section('title', 'page title') is used to fill in the b2386ffb911b14667cb8f0f91ea547a7 tag in the main template. Unlike standard HTML templates that use variables for filling, Blade templates allow us to inherit part of the content of other templates and make the separation of data more obvious.

  1. Use Laravel View static methods

In addition to the @yield() function and @section() function, Laravel also provides View static methods, which is recommended. , the specific implementation steps are as follows:

    <?php

    namespace AppHttpControllers;

    use IlluminateHttpRequest;
    use IlluminateSupportFacadesView;

    class HomeController extends Controller
    {
        public function index()
        {
            $data = [
                'title' => '页面标题',
                'header' => '<h1>头部内容</h1>',
                'nav' => '<ul>
                            <li><a href="#">导航栏1</a></li>
                            <li><a href="#">导航栏2</a></li>
                            <li><a href="#">导航栏3</a></li>
                          </ul>',
                'content' => '<p>主体内容</p>',
                'footer' => '<p>版权信息</p>'
            ];
            return View::make('home.index', $data);
        }
    }

In the above code, we use View::make to generate the view, and pass in an array instance $data as the variable context of the view. In this array, we have defined five variables: $title, $header, $nav, $content, $footer, etc., which are used to fill in the corresponding template blocks in the main template.

  1. Use the control structure in Blade template

In Blade template, in addition to @yield() and @section() to fill template blocks, we can also use control Structures, such as @if, @foreach, @for, etc., to implement specific logic, the specific implementation is as follows:

@section('content')
    <div>
        @foreach ($posts as $post)
            <h2>{{ $post->title }}</h2>
            <p>{{ substr($post->content, 0, 100) }}</p>
        @endforeach
    </div>
@endsection

In this code, we use the @foreach loop statement to traverse the array $posts, and use { { $post->title }} and {{ substr($post->content, 0, 100) }} to output the article title and brief content.

Summary

The above is a practical case demonstration of how to use Laravel Blade template layout. The use of Laravel Blade template layout can greatly improve the development efficiency of WEB applications and also make business logic and The separation of views is more obvious. Of course, in addition to this, the Laravel framework has many powerful features worth exploring.

The above is the detailed content of Laravel development: How to use Laravel Blade template layout?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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