繁体   English   中英

Laravel 8.0 刀片嵌套 @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') 我不知道这个逻辑中的问题。

编辑这里是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.

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

浮动按钮.blade.php:

<div>
  My Float Buttons Code
</div>

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


第二部分

包含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

浮动按钮.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-buttons-help')

暂无
暂无

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

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