【问题标题】:laravel blade template not renderinglaravel 刀片模板不渲染
【发布时间】:2016-04-04 04:27:09
【问题描述】:

进入 laravel 并尝试使用刀片模板但它不渲染。我的所有示例都用于 laravel 文档。

更新
所以这是我的 ma​​ster.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.php 扩展名。

标签: php laravel laravel-5.1 laravel-blade


【解决方案1】:

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

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

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

【讨论】:

  • @helloworld 您在加载页面时呈现的是哪个视图?
  • master.blade.php 位于资源 > 视图 >
  • 这是正确的答案,使用包含,并删除@section('header') 和@endsection
【解决方案2】:

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

@extends('master')

【讨论】:

  • 这只是给我一个空白的白屏
【解决方案3】:

布局:

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

【讨论】:

  • 在布局中,如果你想使用不可变的内容,使用@include('path.to.view'),如果你的内容在每个视图的情况下发生变化,使用@yield,可以改变一些部分布局
【解决方案4】:

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

@extends('master')

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

ma​​ster.blade.php

@yield('header')

header.blade.php(使用@stop 而不是@endsection

@section('header')
    header content
@stop

pastebin.com 链接:

master.blade.php

header.blade.php

【讨论】:

  • 这样做只会给我一个空白屏幕
  • 你的 master.blade.php 在哪里?
  • 资源 > 视图 > master.blade.php
【解决方案5】:

我遇到了类似的问题:

layouts/user-profile.blade.php

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

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

@extends('layouts.user-profile')

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2018-10-03
    • 2016-02-27
    • 2016-03-17
    • 2016-07-13
    • 2018-08-15
    • 1970-01-01
    • 2017-01-02
    • 2013-04-29
    相关资源
    最近更新 更多