网站PHP框架之Laravel5.5(九)Blade模版

14 篇文章 0 订阅

在做一个项目中,网页会越来越多,而网页很大一部分内容其实是相同的,这样会有很多重复性前端代码;为了避免这种情况,很多web开发的框架中都引入了模版概念,laravel也不例外,Laravel中定义的是blade模版。

首先要定义布局模版,为了便于管理,我们先在view目录下创建layouts目录。

cd resources/views/
mkdir layouts

在layouts目录下创建三个blade模版文件:

  • app.blade.php
  • heading.blade.php
  • footer.blade.php

定义主模版

把Laravel给我们的默认welcome模版里面的所有代码复制到app.blade.php里面去。

我们准备把app.blade.php当成主模版,接下来把app.blade.php代码改造一下:

改造后的app.blade.php代码如下:

<!doctype html>
<html lang="{{ app()->getLocale() }}">
<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>Laravel</title>

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">

    <!-- Styles -->
    <style>
        html, body {
            background-color: #fff;
            color: #636b6f;
            font-family: 'Raleway', sans-serif;
            font-weight: 100;
            height: 100vh;
            margin: 0;
        }

        .full-height {
            height: 100vh;
        }

        .flex-center {
            align-items: center;
            display: flex;
            justify-content: center;
        }

        .position-ref {
            position: relative;
        }

        .top-right {
            position: absolute;
            right: 10px;
            top: 18px;
        }

        .content {
            text-align: center;
        }

        .title {
            font-size: 84px;
        }

        .links > a {
            color: #636b6f;
            padding: 0 25px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: .1rem;
            text-decoration: none;
            text-transform: uppercase;
        }

        .m-b-md {
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
<div class="flex-center position-ref full-height">
    @include('layouts.heading')
    <div class="content">
        <div class="title m-b-md">
            @yield('content')
        </div>
        @include('layouts.footer')
    </div>
</div>
</body>
</html>

heading.blade.php:

@if (Route::has('login'))
    <div class="top-right links">
        @auth
            <a href="{{ url('/home') }}">Home</a>
        @else
            <a href="{{ route('login') }}">Login</a>
            <a href="{{ route('register') }}">Register</a>
        @endauth
    </div>
@endif

footer.blade.php:

<div class="links">
    <a href="https://laravel.com/docs">Documentation</a>
    <a href="https://laracasts.com">Laracasts</a>
    <a href="https://laravel-news.com">News</a>
    <a href="https://forge.laravel.com">Forge</a>
    <a href="https://github.com/laravel/laravel">GitHub</a>
</div>

 

现在主模版做好了,那就使用起来!

使用主模版

我们打开view/books/index.blade.php

全选代码并且删除,写上:

@extends('layouts.app')

@section('content')
    {!! $name !!}.{{$website}}
@stop

学过JAVA的都知道extends是继承的意思,这里的@extends是继承主模版的意思,@section和@stop之间的内容是可变内容,里面的内容都会当成模版中的可变区块@yield,值得一提的是@section('A')对应@yield('A'),A=A。

 

现在我们打开Chrome,访问测试链接testdemo.test看到:

这就使用主模版成功了。

 

定义Blade模版的思路:

  1. 提取网页的变化部分和固定部分

  2. 主模版固定部分另外写入固定模块文件里

  3. 主模版中用@include('相应的固定模块文件路径')来引用

  4. 主模版变化部分用@yield('区块名')定义一个区块

  5. 网页入口文件继承主模版后写入@section('对应区块名') 内容 @stop作为变化部分的内容

 

 

 

系列文章:

网站PHP框架之Laravel系列文章

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值