Php 刀片模板-@extends&@部分不工作
我正在学习laravel框架,并试图掌握如何使用刀片模板引擎。然而,我无法在我的项目中使用@extends和@section功能 我已经多次尝试重新安装整个项目,使用不同的浏览器并重新启动我的机器,但我不明白为什么它不显示@section内容 Laravel版本:5.7.28 | IDE:PhpStorm routes/web.phpPhp 刀片模板-@extends&@部分不工作,php,laravel,laravel-blade,Php,Laravel,Laravel Blade,我正在学习laravel框架,并试图掌握如何使用刀片模板引擎。然而,我无法在我的项目中使用@extends和@section功能 我已经多次尝试重新安装整个项目,使用不同的浏览器并重新启动我的机器,但我不明白为什么它不显示@section内容 Laravel版本:5.7.28 | IDE:PhpStorm routes/web.php Route::get('/', function () { return view('layouts/index'); }); Route::get('
Route::get('/', function () {
return view('layouts/index');
});
Route::get('/', function () {
return view('index');
});
视图/布局/index.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@yield('header')
</div>
</body>
@extends('layouts.index')
@section('header')
<p>Header</p>
@endsection
<!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>
@extends('layouts.master')
@section('title', 'Changing the default title')
@section('content')
<p>content displayed</p>
@endsection
站点索引
@收益率('header')
views/header.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@yield('header')
</div>
</body>
@extends('layouts.index')
@section('header')
<p>Header</p>
@endsection
<!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>
@extends('layouts.master')
@section('title', 'Changing the default title')
@section('content')
<p>content displayed</p>
@endsection
@extends('layouts.index'))
@节(“标题”)
标题
@端部
目前显示的只是views/layouts/index.blade.php文件中的标记
非常感谢您在这方面的所有意见。模板不是这样工作的。您必须在return语句中引用子模板。因为
@extends
在这个子模板中,所以Laravel知道使用提到的主布局。因此,您的返回声明如下所示:
return view('header');
如果只想在每页上显示页眉,则不需要在页眉中扩展主布局,只需在主布局中包含页眉部分即可
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@include('header')
</div>
</body>
站点索引
@包括('标题')
我已经测试了视图和布局,它们似乎很有效。检查控制器返回语句。尝试返回视图('header')
感谢大家的回复,现在我了解了刀片模板引擎如何更好地工作,以及我是如何做得不对。对于像我这样感到困惑并遇到以下问题的其他人,请澄清: 当您通过web路由重定向到视图时,该视图必须是从布局主视图扩展而来的子视图 routes/web.php
Route::get('/', function () {
return view('layouts/index');
});
Route::get('/', function () {
return view('index');
});
默认情况下,主文件中的html将显示,并且它是我们正在“查看”的内容
视图/布局/master.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@yield('header')
</div>
</body>
@extends('layouts.index')
@section('header')
<p>Header</p>
@endsection
<!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>
@extends('layouts.master')
@section('title', 'Changing the default title')
@section('content')
<p>content displayed</p>
@endsection
@收益率('标题','未指定的默认标题')
主收割台
@产量(‘含量’)
要处理页面内容,则使用@section(“content”)方法处理索引视图
views/index.blade.php
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@yield('header')
</div>
</body>
@extends('layouts.index')
@section('header')
<p>Header</p>
@endsection
<!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>
@extends('layouts.master')
@section('title', 'Changing the default title')
@section('content')
<p>content displayed</p>
@endsection
@extends('layouts.master'))
@节(“标题”,“更改默认标题”)
@节(“内容”)
显示的内容
@端部
我希望这对其他人有所帮助。您确定在“路线加载”视图中工作正常吗?@D.Khumoyun我非常确定,因为h1标记显示在视图中,而不是在中扩展内容。
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')