[英]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.