网站PHP框架之Laravel5.5(九)Blade模版
2022-08-03 09:57:03
126
{{single.collect_count}}

在做一个项目中,网页会越来越多,而网页很大一部分内容其实是相同的,这样会有很多重复性前端代码;为了避免这种情况,很多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系列文章

回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.user.group_title}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}