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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂