laravel blade 模板未呈现

laravel blade template not rendering

进入 laravel,我正在尝试使用 blade 模板,但它没有呈现。我的所有示例都用于 laravel 文档。

更新
所以这是我的 master.blade.php 文件 位于 resources > views > master.blade.php

<!DOCTYPE html>
<html>
    <head>

        @yield('layouts.header')
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">Test to Laravel 5</div>
            </div>
        </div>
    </body>
</html>

这是我的 header.blade.php 文件,位于 view/layouts/

@section('header')
    <title>cookie monster</title>
    <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
    <style>
        html, body {
            height: 100%;
        }

        body {
            margin: 0;
            padding: 0;
            width: 100%;
            display: table;
            font-weight: 100;
            font-family: 'Lato';
        }

        .container {
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }

        .content {
            text-align: center;
            display: inline-block;
        }

        .title {
            font-size: 96px;
        }
</style>
@endsection

当我尝试呈现我的页面时,即使我现在有内联 css,也没有添加任何样式,因为我只是在测试 blade 模板引擎的工作方式。

您的布局需要扩展一些内容。这样它就可以利用模板中定义的区域。

@extends('master')

您想使用 @include('layouts.header') 而不是 @yield

@yield 是您在主模板上使用的内容,用于指定内容的去向,然后您可以在子视图上定义这些内容。

@include "allows you to easily include a Blade view from within an existing view." - https://laravel.com/docs/5.1/blade

您应该从 master.blade.php 扩展 header.blade.php。 在 header.blade.php

的第一行添加以下行
@extends('master')

然后您可以编辑@yield。产量和截面必须相同。例如:

master.blade.php

@yield('header')

header.blade.php(使用@stop代替@endsection

@section('header')
    header content
@stop

pastebin.com 链接:

master.blade.php

header.blade.php

布局:

    @yield('header')
</head>
<body>
    <div class="container">
        <div class="content">
            @yield('content')
            <div class="title">Test to Laravel 5</div>
        </div>
    </div>
</body>
</html>

现在,你在控制器中使用

return view('someview');

并查看代码

@extend('layout')
@section('header')
<title>cookie monster</title>
@endsection
@section('content')
bla-bla-bla, render me IN content section of layout
@endsection

结果应该是

<!DOCTYPE html>
<html>
<head>

<title>cookie monster</title>
</head>
<body>
    <div class="container">
        <div class="content">
            bla-bla-bla, render me IN content section of layout
            <div class="title">Test to Laravel 5</div>
        </div>
    </div>
</body>
</html>

我遇到了类似的问题:

layouts/user-profile.blade.php

<body>
  @yield('main')
  @yield('footer')
</body>

user-profile/create.blade.php

@extends('layouts.user-profile')

@section('main')
   <!-- include a personal-info sub-section -->
   @include($templateName)
@stop

@section('footer')
  <script></script>
@stop

问题在于此子部分继承需要 blade 的 @parent,因此所有 @section('footer') 块都在 @yield('footer')

中呈现

user-profile/create/个人信息.blade.php

@section('footer')
  @parent
  // subsection scripts
@stop