php - Laravel 8.0 Blade 嵌套 @extends @section 和 @yield 不起作用
问题描述
我有一个带有以下代码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
(accessible at 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
为了简化问题,我需要@section('float-button-module-menu')
从float-buttons.blade.php
内部访问home.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.
解决方案
扩展视图适用于您直接调用/查看刀片文件时,例如通过return view('templates.float-buttons.float-buttons')
. 听起来这不是你要找的。你想要@include
在你的文件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
浮动按钮.blade.php:
<div>
My Float Buttons Code
</div>
float-buttons.blade.php
将只包含浮动按钮的代码
第二部分
您可以在包含子视图@include
时传递参数。查看这些更改是否适合您的需求:
主页.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 -按钮帮助')
推荐阅读
- r - dplyr:在 for 循环中获取分组的最小和最大列
- python - 在 Django Model 数据库函数中使用 Unaccent
- java - 我无法使用 Selenium 启动浏览器
- python - 按值而不是引用在列表中分配值
- smtp - 为什么即使在配置 SMTP 之后 prestashop 电子邮件也无法正常工作?
- algorithm - 从给定数字中获取所有可能的操作组合的算法
- html - 背景图像未使用 CSS 显示
- c++ - 如何为我的游戏设置主菜单场景?QT C++
- node.js - 如何使用 listObjectV2 和 NodeJs 从 S3 AWS 获取对象列表?
- java - 如何找到所有可用的 JDBC 驱动程序?