[英]Laravel Blade @yield is not displaying the @section
I have text that is displayed in two spots (one for desktops and one for mobile). 我的文字显示在两个位置(一个用于桌面,一个用于移动)。 So instead of having to place the text in two spots each time, I thought that I could just make a section and display the text in each spot using
@yield
. 因此,我不必每次都将文本放置在两个位置中,而是想到可以使用
@yield
制作一个节并在每个位置中显示文本。 I am getting no errors, but the content isn't getting written out to the dom as I was expecting it to. 我没有任何错误,但是内容没有像我期望的那样写到dom上。
Here is the blade template: 这是刀片模板:
<nav class="blue">
<div class="nav-wrapper">
<div class="container">
<ul class="side-nav" id="mobile-side-nav">
@yield('navbar-dropdown')
</ul>
</div>
</div>
</nav>
<ul id="nav-dropdown" class="dropdown-content">
@yield('navbar-dropdown')
</ul>
{{-- List of items to place in the ul's --}}
@section('navbar-dropdown')
<li><a class="blue-text waves-effect" href="#!">Create Project</a></li>
<li><a class="blue-text waves-effect" href="#!">Manage Projects</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Settings</a></li>
<li><a class="blue-text waves-effect" href="{{ url('auth/logout') }}">Logout</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Help</a></li>
<li><a class="blue-text waves-effect" href="#!">Report a Problem</a></li>
@endsection
Am I misunderstanding how this works, or am I just doing it wrong? 我是误解了它的工作原理,还是我做错了?
@yield
only works when extending a layout. @yield
仅在扩展布局时有效。
You would be much better off putting the contents of your narbar-dropdown
section in another file and then using include. 您最好将
narbar-dropdown
部分的内容放在另一个文件中,然后使用include。
Eg create a file at resources/views/partials/navbar-dropdown.blade.php
and add: 例如,在
resources/views/partials/navbar-dropdown.blade.php
创建一个文件,并添加:
<li><a class="blue-text waves-effect" href="#!">Create Project</a></li>
<li><a class="blue-text waves-effect" href="#!">Manage Projects</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Settings</a></li>
<li><a class="blue-text waves-effect" href="{{ url('auth/logout') }}">Logout</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Help</a></li>
<li><a class="blue-text waves-effect" href="#!">Report a Problem</a></li>
and then in your layouts file you would change: 然后在布局文件中进行更改:
@yield('navbar-dropdown')
to 至
@include('partials/navbar-dropdown')
Hope this helps! 希望这可以帮助!
I think you didn't extend the layout. 我认为您没有扩展布局。 When you use
@yield
and @section
you need to define the @yield
in your layout that you want to extend. 使用
@yield
和@section
,需要在要扩展的布局中定义@yield
。
This is your template file (called Laravel/resources/views/layouts/app.blade.php
): 这是您的模板文件(称为
Laravel/resources/views/layouts/app.blade.php
):
<html>
<nav class="blue">
<div class="nav-wrapper">
<div class="container">
<ul class="side-nav" id="mobile-side-nav">
@yield('navbar-dropdown')
</ul>
</div>
</div>
</nav>
<ul id="nav-dropdown" class="dropdown-content">
@yield('navbar-dropdown')
</ul>
</html>
And this is your file that will extend the template: 这是您将扩展模板的文件:
@extends('layouts.app')
@section('navbar-dropdown')
<li><a class="blue-text waves-effect" href="#!">Create Project</a></li>
<li><a class="blue-text waves-effect" href="#!">Manage Projects</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Settings</a></li>
<li><a class="blue-text waves-effect" href="{{ url('auth/logout') }}">Logout</a></li>
<li class="divider"></li>
<li><a class="blue-text waves-effect" href="#!">Help</a></li>
<li><a class="blue-text waves-effect" href="#!">Report a Problem</a></li>
@endsection
Hope this works! 希望这有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.