laravel(六)-Blade模板

来源:互联网 发布:网络应用层协议有哪些 编辑:程序博客网 时间:2024/06/05 15:33

  • Blade模板
    • 简介
    • 模板继承
    • 详细使用

Blade模板

简介

Blade 是 Laravel 提供的一个非常简单、强大的模板引擎,Blade 在视图中不约束 PHP 原生代码。所有的 Blade 视图都会被编译成原生 PHP 代码并缓存起来直到被修改,这意味着对应用的性能而言 Blade 基本上是零开销。Blade 视图文件使用 .blade.php 文件扩展,默认存放在 resources/views 目录下。

模板继承

  • section 定义了一个内容的片段

  • yield 用于显示给定片段的内容

  • extends 指定子页面所继承的布局

  • parent @parent 指令在视图渲染时将会被布局中的内容替换 ,可理解为类中调用父类方法的parent::action();

详细使用

<!--layout.blade.php 布局--><!doctype html><html lang="en"><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','默认值')</title></head><style>    header,footer {        width: 100%;        height: 300px;        background-color: khaki;    }    aside {        float: left;        width: 45%;        margin-left: 5%;        height: 500px;        background-color: lavenderblush;    }    .clear {        display: block;        height: 0;        width: 100%;        clear: both;    }</style><body>    <header>头部@yield('header','默认值')</header>    <aside class="left">        @section('left-aside')            左侧            {{ $title }}        @show    </aside>    <aside class="right">        @section('right-aside')            右侧        @show    </aside>    <div class="clear"></div>    <footer>        @section('footer')            尾部        @show    </footer></body></html>
<!--nav.blade.php 独立出来的组件,供include指令调用--><nav>    <ul>        <li>1</li>        <li>2</li>        <li>3</li>        <li>4</li>        <li>5</li>        <li>使用变量{{ $title }}</li>    </ul></nav>
<!--son.blade.php 模板 继承自layout-->{{--继承layout文件--}}@extends('layout'){{--重写title--}}@section('title')    {{--1. 输出变量--}}    {{ $title }}@stop@section('header')    header2    <h3>模板中生成url</h3>    <ol>        <li>url 通过路由名称生成url {{ url('/nice') }}</li>        <li>action 通过控制器及方法生成url {{ action('Member\IndexController@index') }}</li>        <li>route 通过路由别名生成url {{ route('niceas') }}</li>    </ol>@stop@section('left-aside')    重写父模板内容@stop@section('right-aside')    @parent    继承父模板内容并追加内容    {{--1. 输出变量--}}    {{ $title }}    {{--2. 使用PHP代码--}}    {{ time() }}    {{ var_dump($title) }}    {{--下面两种三元表达式等价--}}    {{ isset($header) ? $header : '默认值' }}    {{ $header or '默认值' }}    {{--3. 原样输出--}}    @{{ $title }}    {{--4. Blade 还允许你在视图中定义注释,然而,不同于 HTML 注释,Blade 注释并不会包含到 HTML 中被返回--}}    {{--5. 引入子视图--}}    @include('nav')    {{--6. 流程控制--}}    <!-- if-else -->    @if(1 > 1)        1不等于0    @elseif(1 < 1)        1不等于2    @else        1等于1    @endif    <!-- 相当于if取反 -->    @unless(1!=1)        输出    @endunless    <!-- for -->    @for($i = 1;$i<10;$i++)        {{ $i }}    @endfor    <!-- foreach -->    @foreach ($users as $user)        <p>This is user {{ $user->id }}</p>    @endforeach    @forelse ($users as $user)        <li>{{ $user->name }}</li>    @empty        <p>No users</p>    @endforelse    <!-- while -->    @while ($i>0)        <p>I'm looping forever.{{ $i }}</p>        {{ $i-- }}    @endwhile@endsection
原创粉丝点击