Laravel 刀片模板未渲染

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34525416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:55:46  来源:igfitidea点击:

laravel blade template not rendering

phplaravellaravel-5.1laravel-blade

提问by hello world

Getting into laravel and im trying to work with blade templating but its not rendering. All my examples are coming for the laravel documentation.

进入 laravel,我正在尝试使用刀片模板,但它没有渲染。我所有的例子都是为 laravel 文档而来的。

UPDATE
So here is my master.blade.php filelocated in resources > views > master.blade.php

更新
所以这是我的master.blade.php 文件,位于资源 > 视图 > 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>

and here is my header.blade.php file located in view/layouts/

这是我的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

when i try to render my page no styles are being added even tho i have inline css at the moment, because i'm just testing how blade template engine works.

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

回答by James

You want to be using @include('layouts.header')rather than @yield.

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

@yieldis what you use on a master template to specify where content will go that you can then define on a child view.

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

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

@include“允许您从现有视图中轻松包含 Blade 视图。” - https://laravel.com/docs/5.1/blade

回答by Ohgodwhy

Your layout needs to extend something. That way it can take advantage of the defined regions in your template.

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

@extends('master')

回答by Peter Kota

You should extend the header.blade.phpfrom master.blade.php. Add the following line to the first line of header.blade.php

您应该扩展header.blade.phpfrom master.blade.php。将以下行添加到第一行header.blade.php

@extends('master')

Then you may edit the @yield. The yield and the section must be the same. For example:

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

master.blade.php

master.blade.php

@yield('header')

header.blade.php(use @stopinstead of @endsection)

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

@section('header')
    header content
@stop

pastebin.com links:

pastebin.com 链接:

master.blade.php

master.blade.php

header.blade.php

header.blade.php

回答by KoIIIeY

layout:

布局:

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

now, you use in controller

现在,您在控制器中使用

return view('someview');

and view code

并查看代码

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

and result should be

结果应该是

<!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>

回答by Gus

I was facing a similar problem where:

我遇到了类似的问题,其中:

layouts/user-profile.blade.php

布局/用户配置文件.blade.php

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

user-profile/create.blade.php

用户配置文件/create.blade.php

@extends('layouts.user-profile')

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

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

The problem was with this sub-section inheritance where the need of blade's @parentwas needed so all of the @section('footer')blocks were rendered in the @yield('footer')

问题在于这个子部分的继承需要刀片的@parent需要,所以所有的@section('footer')块都在@yield('footer')

user-profile/create/personal-info.blade.php

用户配置文件/创建/个人信息.blade.php

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