首页 > 解决方案 > Laravel 8.0 Blade 嵌套 @extends @section 和 @yield 不起作用

问题描述

我有一个带有以下代码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(accessible at 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

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

编辑这里是里面部分的完整代码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.

标签: phplaravelframeworks

解决方案


扩展视图适用于您直接调用/查看刀片文件时,例如通过return view('templates.float-buttons.float-buttons'). 听起来这不是你要找的。你想要@include在你的文件home.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

浮动按钮.blade.php:

<div>
  My Float Buttons Code
</div>

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


第二部分

您可以在包含子视图@include时传递参数。查看这些更改是否适合您的需求:

主页.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

浮动按钮.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'])在任何需要的地方使用它。当然可以随意扩展列表


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


推荐阅读