简体   繁体   English

Laravel 8.0 刀片嵌套 @extends @section 和 @yield 不起作用

[英]Laravel 8.0 Blade nested @extends @section and @yield not working

I have this template.blade.php with a path of views/templates/template.blade.php with the following code:我有这个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

Now, in my route, the home.blade.php (accessible at views/home/home.blade.php ) was set as the landing page.现在,在我的路线中,将home.blade.php (可在views/home/home.blade.php )设置为登录页面。 The homepage needs to borrow a section of code (code for the floating buttons. Each floating button differs depending on the current page).首页需要借用一段代码(浮动按钮的代码。每个浮动按钮根据当前页面的不同而不同)。 Here's the code for the homepage:这是主页的代码:

// 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

The @yield('float-button-module-menu') would be accessed at views/templates/float-buttons/float-buttons.blade.php . @yield('float-button-module-menu')可以在views/templates/float-buttons/float-buttons.blade.php访问。 Here's the code for the said webpage:这是所述网页的代码:

// 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

To simplify the question, I need to access the @section('float-button-module-menu') from float-buttons.blade.php inside home.blade.php .为了简化问题,我需要从home.blade.php内部的float-buttons.blade.php访问@section('float-button-module-menu') I do not know the problem within the logic of this.我不知道这个逻辑中的问题。

EDIT here's the full code for the sections inside float-button.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.

Extending views is for when you call/view a blade file directly, for example via return view('templates.float-buttons.float-buttons') .扩展视图适用于直接调用/查看刀片文件时,例如通过return view('templates.float-buttons.float-buttons') Sounds like this is not what you are looking for.听起来这不是你要找的。 You'd want to @include the file in your home.blade.php .您想在home.blade.php@include文件。 Make the following changes:进行以下更改:

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

float-buttons.blade.php:浮动按钮.blade.php:

<div>
  My Float Buttons Code
</div>

float-buttons.blade.php will just contain the code for your float buttons float-buttons.blade.php将只包含浮动按钮的代码


Second part第二部分

You can pass parameters to @include when including subviews .包含subviews 时,您可以将参数传递给@include See if these changes suit your needs:查看这些更改是否适合您的需求:

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', ['type' => 'anchor'])
@endsection

float-buttons.blade.php:浮动按钮.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

You can then basically use @include('templates.float-buttons.float-buttons', ['type' => 'anchor']) or @include('templates.float-buttons.float-buttons', ['type' => 'help']) wherever you need it.然后,您基本上可以使用@include('templates.float-buttons.float-buttons', ['type' => 'anchor'])@include('templates.float-buttons.float-buttons', ['type' => 'help'])任何你需要的地方。 And of course feel free to expand the list当然可以随意扩展列表


Note: A more elegant solution imho would be to create single blade files for each of your cases ('float-buttons-anchor.blade.php , float-buttons-help.blade.php ...) and then include the file you need: @include('templates.float-buttons.float-buttons-help')注意:恕我直言,一个更优雅的解决方案是为每个案例创建单个刀片文件('float-buttons-anchor.blade.php , float-buttons-help.blade.php ...) and then include the file you need: @include('templates.float-buttons.float-buttons-help')

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

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