dongpao5127 2016-11-29 19:39
浏览 86
已采纳

使用Laravel Framework - 我的layout.blade.php文件中只有1个yield

The issue is that my about.blade.php file shows in the layout, but my navbar.blade.php does not.

The best I can guess (I'm new to Laravel), is that my controller returns about.blade.php which extends layout.blade.php, but for some reason, layout.blade does not know about navbar.blade.php even though navbar.blade.php extends layout.blade.php.

layout.blade.php

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>

      @yield('navbar')

        <div class="container">

          @yield('content')

        </div>

    </body>
</html>

navbar.blade.php

@extends('layout')
@section('navbar')

<header>
  <nav class="nav navbar-default navbar-fixed-top">
    <div class="container-fluid">
      <div class="navbar-header">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-1" aria-expanded="false">
          <span class="sr-only">Toggle Navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand" href="/?page=main">SWMHS</a>
      </div>

      <div class="collapse navbar-collapse" id="navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li><a href="/">Main</a></li>
          <li><a href="/about">About</a></li>
        </ul>
      </div>

    </div>
  </nav>
</header>

@endsection

about.blade.php

@extends('layout')

@section('content')

  <h1 class="text-center">This is the About Page</h1>

@endsection
  • 写回答

1条回答 默认 最新

  • duanmiaosi0150 2016-11-29 20:04
    关注

    The yield directive doesn't work the way you're thinking it does. It will only display the content if the section has been added to the blade template.

    You would need to add a navbar section and copy and paste it section into every view that you'd like it to show in. An alternative way is to create a partial and @include() the navbar template.

    Here's the way it should be done, assuming your navbar template isn't going to change.

    layout.blade.php

    <!DOCTYPE html>
    <html>
        <head>
        </head>
        <body>
    
            @include('partials.navbar')
    
            <div class="container">
                @yield('content')
            </div>
    
        </body>
    </html>
    

    partials/navbar.blade.php

    <header>
        <nav class="nav navbar-default navbar-fixed-top">
            <div class="container-fluid">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle Navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="/?page=main">SWMHS</a>
                </div>
    
                <div class="collapse navbar-collapse" id="navbar-collapse-1">
                    <ul class="nav navbar-nav">
                        <li><a href="/">Main</a></li>
                        <li><a href="/about">About</a></li>
                    </ul>
                </div>
    
            </div>
        </nav>
    </header>
    

    about.blade.php

    @extends('layout')
    
    @section('content')
    
    <h1 class="text-center">This is the About Page</h1>
    
    @endsection
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#stm32#的问题:第一张图是发送数据代码,第二第三张图是接收数据并进行DAC转换的函数,若用串口一接收数据并进行DAC转换,主函数代码该怎么写
  • ¥20 hc130怎么读写内部flash存储信息
  • ¥15 Axure rp9注册与登录交互
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面