亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

PHP
呼啦一陣風 2021-08-28 18:26:58
我正在學習 Laravel 框架并嘗試使用刀片模板引擎。但是,我終生無法在我的項目中使用 @extends 和 @section 功能。我已經嘗試多次重新安裝整個項目,使用不同的瀏覽器并重新啟動我的機器,但我無法弄清楚為什么它不顯示 @section 內容Laravel 版本:5.7.28 | IDE:PhpStorm路線/ web.phpRoute::get('/', function () {    return view('layouts/index');});視圖/布局/index.blade.php<body><div class="container-fluid">    <h1>Site Index</h1>    @yield('header')</div></body>意見/header.blade.php@extends('layouts.index')@section('header')    <p>Header</p>@endsection目前顯示的只是 views/layouts/index.blade.php 文件中的標簽。非常感謝您對此提出的任何意見。
查看完整描述

3 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

這不是模板的工作方式。您必須在 return 語句中引用子模板。因為@extends在這個子模板中,Laravel 知道使用提到的主布局。所以你的退貨聲明應該是這樣的:


return view('header');

如果您只想在每個頁面上顯示頁眉,則無需在頁眉中擴展主布局,只需在主布局中包含頁眉部分即可。


<body>

<div class="container-fluid">

    <h1>Site Index</h1>

    @include('header')

</div>

</body>


查看完整回答
反對 回復 2021-08-28
?
牧羊人nacy

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')  


查看完整回答
反對 回復 2021-08-28
?
萬千封印

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

我希望這對其他人有幫助。


查看完整回答
反對 回復 2021-08-28
  • 3 回答
  • 0 關注
  • 305 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號