laravel:blade模板的继承(10.27.0)

发布时间 2023-10-29 10:36:27作者: 刘宏缔的架构森林

一,相关文档

https://learnku.com/docs/laravel/10.x/blade/14852#918cb6

二,模板:

resources/views/layouts/layout.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>@yield('title')|老刘的博客</title>
    <link rel="shortcut icon" type="image/x-ico" sizes="16x16" href="/favicon.ico"/>
    <meta name="keywords" content="SevDot,独立开发,自由职业"/>
    <meta name="description" content="崇尚独立开发,追求自由职业,记录在独立自由上的探索。"/>
</head>
 
<body>
@include('layouts.header')
@yield('content')
@include('layouts.footer')
</body>
</html>

resources/views/layouts/header.blade.php

1
2
3
<div id="header" style="width:100%;height:100px;background: #ffff00;">
    这里是头部
</div>

resources/views/layouts/footer.blade.php

1
2
3
4
5
6
7
8
<div id="footer" style="width:100%;height:100px;background: #00ff00;">
    这里是底部:友情链接:<br/>
    <ul>
        @foreach($links as $v)
            <li><a href="{{$v['url']}}">{{$v['name']}}</a></li>
        @endforeach
    </ul>
</div>

resources/views/pages/home.blade.php

1
2
3
4
5
6
7
8
9
10
@extends('layouts.layout')
@section('title')
    {{$title}}
@stop
@section('content')
    <div>
        这里是主要内容区<br/>
        商品名称:{{$goodsName}}
    </div>
@stop

三,php代码:

controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//显示home页面
public function home(Request $request){
 
    $goodsName = '元青花宝瓶仿制品';
 
    $links = [
        ["name"=>"新浪","url"=>"https://www.sina.com.cn"],
        ["name"=>"百度","url"=>"https://baidu.com"],
    ];
 
    return view('pages.home', [
        'goodsName' => $goodsName,
        'title' => 'laravel教学网',
        'links' => $links,
    ]);
}

说明:刘宏缔的架构森林—专注it技术的博客,
网站:https://blog.imgtouch.com
原文: https://blog.imgtouch.com/index.php/2023/10/28/laravel-blade-mu-ban-de-ji-cheng-10-27/
代码: https://github.com/liuhongdi/ 或 https://gitee.com/liuhongdi
说明:作者:刘宏缔 邮箱: 371125307@qq.com

四,测试效果:

五,查看laravel框架的版本:

liuhongdi@lhdpc:/data/laravel/dignews$ php artisan --version
Laravel Framework 10.27.0