3 回答

TA貢獻1817條經驗 獲得超6個贊
這不是模板的工作方式。您必須在 return 語句中引用子模板。因為@extends在這個子模板中,Laravel 知道使用提到的主布局。所以你的退貨聲明應該是這樣的:
return view('header');
如果您只想在每個頁面上顯示頁眉,則無需在頁眉中擴展主布局,只需在主布局中包含頁眉部分即可。
<body>
<div class="container-fluid">
<h1>Site Index</h1>
@include('header')
</div>
</body>

TA貢獻1862條經驗 獲得超7個贊
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')

TA貢獻1891條經驗 獲得超3個贊
現在我明白了刀片模板引擎如何工作得更好一點,以及我是如何做錯的。只是為了澄清其他像我一樣感到困惑并遇到這個線程的人:
當您通過 Web 路由重定向到視圖時,它必須是從布局母版擴展的子級。
路線/ web.php
Route::get('/', function () {
return view('index');
});
然后將默認顯示主文件中的 html 及其我們正在“查看”的內容
視圖/布局/master.blade.php
<!doctype html>
<html>
<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>
要處理頁面的內容,然后使用 @section('content') 方法處理它的索引視圖。
意見/index.blade.php
@extends('layouts.master')
@section('title', 'Changing the default title')
@section('content')
<p>content displayed</p>
@endsection
我希望這對其他人有幫助。
- 3 回答
- 0 關注
- 305 瀏覽
添加回答
舉報