简体   繁体   English

刀片模板-@extends和@section不起作用

[英]Blade template - @extends & @section not working

I'm learning the laravel framework and trying to get to grips with using the blade template engine. 我正在学习laravel框架,并尝试使用刀片模板引擎。 However i cant for life of me get the @extends and @section functionality to work within my project. 但是我无法终生获得@extends和@section功能在我的项目中工作。

I have already tried reinstalling the whole project multiple times, using different browsers and restarting my machine but i cant figure out why it doesn't display the @section content 我已经尝试使用不同的浏览器多次重新安装整个项目,然后重新启动计算机,但是我不知道为什么它不显示@section内容

Laravel Version: 5.7.28 | Laravel版本:5.7.28 | IDE: PhpStorm IDE:PhpStorm

routes/web.php 路线/web.php

Route::get('/', function () {
    return view('layouts/index');
});

views/layouts/index.blade.php views / layouts / index.blade.php

<body>
<div class="container-fluid">
    <h1>Site Index</h1>
    @yield('header')
</div>
</body>

views/header.blade.php views / header.blade.php

@extends('layouts.index')

@section('header')
    <p>Header</p>
@endsection

At the moment all that is being displayed is the tag in the views/layouts/index.blade.php file. 目前,所显示的只是views / layouts / index.blade.php文件中的标签。

Thank you very much for any and all input on this. 非常感谢您对此的任何投入。

That's not how the templating works. 那不是模板的工作原理。 You have to reference the child template in your return statement. 您必须在return语句中引用子模板。 Because the @extends is in this child template, Laravel knows to use the mentioned master layout. 因为@extends在此子模板中,所以Laravel知道使用提到的主布局。 So your return statement would be like so: 因此,您的return语句将如下所示:

return view('header');

If you just want the header to be displayed on every page, you don't need to extend the master layout in your header, you should just include the header part in your master layout. 如果只希望标题显示在每个页面上,则无需在标题中扩展主版面,只需将标题部分包括在主版面中即可。

<body>
<div class="container-fluid">
    <h1>Site Index</h1>
    @include('header')
</div>
</body>

i have tested the view and layout they seems working. 我已经测试了它们似乎正常工作的视图和布局。 check your controller return statement. 检查您的控制器退货声明。 try return view('header') ; 尝试return view('header') ;

Route::get('/', function () {
    return view('header');
});

thanks all for your responses, now i understand how the blade template engine works a little better and how i was doing this wrong. 感谢大家的回应,现在我了解了刀片模板引擎如何更好地工作以及我做错了什么。 Just for clarification for others that get confused like me and come across this thread: 只是为了澄清那些像我一样困惑并遇到此问题的人:

When you are redirecting to a view through the web routes then it has to be a child that is extending from a layouts master. 当您通过网络路由重定向到视图时,它必须是从布局母版扩展的子代。

routes/web.php 路线/web.php

Route::get('/', function () {
    return view('index');
});

The html from the master file will then be displayed by default and its the content that we are "viewing" 然后,默认情况下将显示主文件中的html及其我们正在“查看”的内容

views/layouts/master.blade.php views / layouts / master.blade.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>@yield('title', 'default title if unspecified')</title>
</head>
<body>
    <h1>Master Header</h1>
    @yield('content')
</body>
</html>

To work with the content of the page then its the index view that is worked with using the @section('content') method. 要使用页面的内容,则使用@section('content')方法使用其索引视图。

views/index.blade.php views / index.blade.php

@extends('layouts.master')

@section('title', 'Changing the default title')

@section('content')
    <p>content displayed</p>
@endsection

I hope this helps for anyone else. 我希望这对其他人有帮助。

If you want to show content of section('header') then you must return header view like

Route::get('/', function () {
    return view('header');
});

this is because contents are in header view and you have been extending layout.index

so if you return layout.index view you will not see content of section('header')  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM