Laravel 8.0 Blade 嵌套@extends @section 和@yield 不工作

Laravel 8.0 Blade nested @extends @section and @yield not working

我有这个 template.blade.php,路径为 views/templates/template.blade.php,代码如下:

// template.blade.php


// some codes here

@include("templates.navigation.navigation")
@yield('header')
@yield('content')
@include('templates.footer.footer')

// some other codes here

现在,在我的路线中,home.blade.php(可在 views/home/home.blade.php 访问)被设置为登录页面。首页需要借用一段代码(浮动按钮的代码,每个浮动按钮根据当前页面不同而不同)。这是主页的代码:

// home.blade.php


@extends('templates.template')
@section('header')
    // some codes here
@endsection

@section('content')
    // some other codes here
    @yield('float-button-module-menu') // the code that needs to be accessed but doesn't work
@endsection

@yield('float-button-module-menu') 将在 views/templates/float-buttons/float-buttons.blade.php 处访问。这是上述网页的代码:

// float-buttons.blade.php


@extends('home.home') // I suspect that I was wrong at declaring the name for the @extends here
@section('float-button-module-menu')
    // some codes inside the section that needs to be displayed
@endsection

为了简化问题,我需要从 home.blade.php 中的 float-buttons.blade.php 访问 @section('float-button-module-menu')。不知道这个逻辑里面的问题。

EDIT 这是 float-button.blade.php:

中各部分的完整代码
// float-button.blade.php


@extends('home.home')
@section('float-button-module-menu')
<div class="d-flex flex-column position-fixed button button-float-position-buttom-left">
    <div class="button-float d-flex">
        <span class="fas fa-user-cog text-lg text-white align-self-center mx-auto"></span>
    </div>
</div>
@endsection

@section('float-button-anchor')
<div class="button-float d-flex">
    <span class="fas fa-arrow-up text-lg text-white align-self-center mx-auto"></span>
</div>
@endsection

@section('float-button-help')
<div class="button-float d-flex">
    <span class="fas fa-question text-lg text-white align-self-center mx-auto"></span>
</div>
@endsection

// list of buttons would be updated.

扩展视图适用于 call/view 直接 blade 文件,例如通过 return view('templates.float-buttons.float-buttons')。听起来这不是你要找的。您想要 @includehome.blade.php 中的文件。进行以下更改:

home.blade.php:

@extends('templates.template')
@section('header')
    // some codes here
@endsection

@section('content')
    // some other codes here
    @include('templates.float-buttons.float-buttons')
@endsection

float-buttons.blade.php:

<div>
  My Float Buttons Code
</div>

float-buttons.blade.php 将只包含浮动按钮的代码


第二部分

including subviews时,您可以将参数传递给@include。查看这些更改是否符合您的需求:

home.blade.php

@extends('templates.template')
@section('header')
    // some codes here
@endsection

@section('content')
    // some other codes here
    @include('templates.float-buttons.float-buttons', ['type' => 'anchor'])
@endsection

float-buttons.blade.php:

@if ($type == 'anchor')
<div class="button-float d-flex">
    <span class="fas fa-arrow-up text-lg text-white align-self-center mx-auto"></span>
</div>

@elseif ($type == 'help')
<div class="button-float d-flex">
    <span class="fas fa-question text-lg text-white align-self-center mx-auto"></span>
</div>

@else
{{-- if no type parameter is given --}}
<p>
  Parameter 'type' is required
</p>
@endif

然后您基本上可以在任何需要的地方使用 @include('templates.float-buttons.float-buttons', ['type' => 'anchor'])@include('templates.float-buttons.float-buttons', ['type' => 'help'])。当然可以随意扩展列表


注意:恕我直言,一个更优雅的解决方案是为每个案例创建单个 blade 文件('float-buttons-anchor.blade.php, float-buttons-help.blade.php...) and then include the file you need:@include('templates.float-buttons.float-buttons-help')