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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?