简体   繁体   English

Laravel 的 Blade 模板 @yield 和 @section 是如何工作的?

[英]Laravel's Blade template @yield and @section how does it work?

I'm new to Laravel and want to learn how to use the Blade template system properly, but i cant wrap my head around the difference between @section and @yield.我是 Laravel 的新手,想学习如何正确使用 Blade 模板系统,但我无法理解 @section 和 @yield 之间的区别。 I've been reading the docs : https://laravel.com/docs/5.7/blade .我一直在阅读文档: https : //laravel.com/docs/5.7/blade

But it's not explaining the differences and how to use them properly.但这并没有解释差异以及如何正确使用它们。 I've been reading posts in other forums too like this one :我一直在阅读其他论坛上的帖子,就像这样:

https://laravel.io/forum/09-02-2014-using-section-and-yield https://laravel.io/forum/09-02-2014-using-section-and-yield

But still i'm a bit confused.但我还是有点困惑。

For example right now i'm creating an app that have multiple pages with commun pieces between them, so for now i get that i have to create a common layout for this pages, but when to use @section and when do i have to use @yield ?例如,现在我正在创建一个应用程序,它有多个页面,它们之间有公共部分,所以现在我知道我必须为此页面创建一个通用布局,但是何时使用 @section 以及何时必须使用@屈服 ?

for example if i have a page like so :例如,如果我有一个这样的页面:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <title>{{ config('app.name', 'Name') }}</title>
    //Common CSS between pages
    <link href="{{ asset('css/style1.css') }}" rel="stylesheet">
    //Changing CSS between pages
    <link href="{{ asset('css/style2.css') }}" rel="stylesheet">
</head>
<body>
    //the content stay the same !
    <div id="app">
        <span id="some_style">hello world !</span>
    </div>
    <script>
       //common JS
       <script src="{{ asset('script1.js') }}">
       //Changing JS between pages
       <script src="{{ asset('script2.js') }}">
    </script>
</body>
</html>

How can i organise it using the blade templating?我如何使用刀片模板组织它?

Assuming you 2 templates. 假设您有2个模板。 Lets call one Base.blade.php and the other one Posts.blade.php . 让我们调用一个Base.blade.php和另一个Posts.blade.php

We'll @extends('base') in Posts . 我们将在Posts @extends('base')

Using @section in Posts and @yield in Base . Posts使用@section和在Base使用@yield

Something like this: 像这样:

Base

@yield('posts') {# the section called "posts" #}


Posts

@extends('base')

@section('posts')
   Here be posts
@endsection

Whatever is written in posts will be yielded in the base blade. 无论写在posts都将在基本刀片中yielded

Think of it as inheritance. 将其视为继承。

You can imagine it as classes if you will. 您可以将它想象成类。 Where the child class calls a method in the base class. 子类在基类中调用方法的位置。

class Base {
    protected function printSomething($something) {
        echo $something;
    }
}


class Posts extends Base {
    public function BaseWillPrint() {
        $this->printSomething('POSTS');
    }
}

Basically I haven't told you anything that doesn't already exist in the documentation. 基本上,我没有告诉过您文档中尚不存在的任何内容。

Yes, assuming if you have a template you can make the base of template in layouts/app.blade/php and you can make the @yield('anything') and then in your views/main.blade.php you must give @extends('layouts.app') and是的,假设你有一个模板,你可以在layouts/app.blade/php中制作模板的基础,你可以制作@yield('anything')然后在你的views/main.blade.php你必须给@extends('layouts.app')

@section('anything')
*for dynamic page
@endsection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM