【问题标题】:Laravel 8.0 Blade nested @extends @section and @yield not workingLaravel 8.0 Blade 嵌套 @extends @section 和 @yield 不起作用
【发布时间】:2021-03-29 03:31:28
【问题描述】:

我有这个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

为了简化问题,我需要从float-buttons.blade.php 中的home.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.

【问题讨论】:

  • 您是否在某个地方调用/查看float-buttons.blade.php 本身?听起来你想在你的 home.blade.php 里面 @include('float-buttons')
  • @brombeer 我试过include('template.float-buttons.float-buttons') 没有@section()@extends()float-buttons.blade.php 中,但不是@include('float-buttons')。我在@include 中声明名称的方式有什么问题吗?
  • 不,听起来不错。确保您的 float-buttons.blade.php 仅包含您要显示的代码,从中删除 @extends@section
  • 如果我将@extends@section 放在float-buttons.blade.php 中,那么我应该如何在home.blade.php 中调用float-buttons.blade.php

标签: php laravel frameworks


【解决方案1】:

扩展视图适用于您直接调用/查看刀片文件时,例如通过return view('templates.float-buttons.float-buttons')。听起来这不是你要找的。你想@includehome.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 将只包含浮动按钮的代码


第二部分

including 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' =&gt; 'anchor'])@include('templates.float-buttons.float-buttons', ['type' =&gt; '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')

【讨论】:

  • 这个逻辑没问题,但我需要将float button codes 作为部分访问,以便我可以重复使用系统中将重复的部分。我已经尝试了该解决方案,但它会使我的系统膨胀编辑它不会使系统膨胀,但我需要根据页面动态访问这些部分。
  • 对不起,我不明白这个。您是否有多个浮动按钮代码并想选择显示哪个?!
  • 还是有点困惑,抱歉 ;) 如果我理解你是正确的,浮动按钮中有多个部分,你需要根据你所在的页面显示不同的部分吗?就个人而言,我会为每个用例创建 one 模板,然后让float-buttons.blade.php 处理要显示的模板。您能否显示float-buttons.blade.php 实际包含的内容?
  • 保证你慢慢来
  • 在我的回答中添加了“第二部分”部分,看看是否适合你。
猜你喜欢
  • 1970-01-01
  • 2021-07-06
  • 2017-05-16
  • 2017-11-18
  • 2017-07-13
  • 2017-12-12
  • 2019-04-18
  • 2017-11-18
  • 2018-04-25
相关资源
最近更新 更多