簡體   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