laravel 5.5 -- blade

blade 模版

拓展 Blade

# class AppServiceProvider 中 boot 方法
Blade::directive('datetime', function ($expression) {
    return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
});
<!-- some.blade.php -->
@datetime($var)
// <?php echo ($var)->format('m/d/Y H:i'); ?>

自定义模版语法

use Illuminate\Support\Facades\Blade;

public function boot()
{
    Blade::if('env', function ($environment) {
        return app()->environment($environment);
    });
}
@env('local')
    // The application is in the local environment...
@else
    // The application is not in the local environment...
@endenv

堆栈

<!-- extend.blade.php -->
@push('scripts')
    <script src="/example.js"></script>
@endpush

/**  输出 start
<script src="/example.js"></script>
<script src="/example.js"></script>
     输出 end  **/
<!-- common.blade.php -->
@stack('scripts')
@stack('scripts')

服务注入

@inject('metrics', 'App\Services\MetricsService')

<div>
    Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>

继承

<!-- layouts/app.blade.php -->
@yield('title')

@section('sidebar')
这是 master 的侧边栏。
@show
<!-- child.blade.php -->
@extends('layouts.app')

@section('title',' Here is a div ')
// 等同于
@section('title') Here is a div @endsection

@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
@endsection

slot

<!-- alert.blade.php -->
<div class="alert alert-danger">
    {{ $slot }}
</div>

<!-- other.blade.php -->
@component('alert')
    <strong>哇!</strong> 出现了一些问题!
@endcomponent
<!-- alert.blade.php -->
<div class="alert alert-danger">
    <div class="alert-title">{{ $title }}</div>
    {{ $slot }}
</div>

<!-- other.blade.php -->  
@component('alert')
    @slot('title')
        拒绝
    @endslot
    你没有权限访问这个资源!
@endcomponent

/**  输出 start
<div class="alert alert-danger">
    <div class="alert-title">拒绝</div>
    你没有权限访问这个资源!
</div>
     输出 end  **/
@component('alert', ['foo' => 'bar'])
    ...
@endcomponent
# 所有的数据都将以变量的形式传递给组件模版 alert.blade.php

Blade & JavaScript 框架

@{{ name }}
# 最终输出 {{ name }} ,供一些 js 框架使用。 

如果是大量的需要这么做,可以使用

@verbatim
    <div class="container">
        Hello, {{ name }}.
    </div>
@endverbatim

控制结构

@unless (Auth::check())
    你尚未登录。
@endunless
// 和 if 相反
@for ($i = 0; $i < 10; $i++)
    目前的值为 {{ $i }}
@endfor

@foreach ($users as $user)
    <p>此用户为 {{ $user->id }}</p>
@endforeach

@forelse ($users as $user)
    <li>{{ $user->name }}</li>
@empty          // 指的是 $users
    <p>没有用户</p>
@endforelse

@while (true)
    <p>死循环了。</p>
@endwhile
@isset($records)
    // $records is defined and is not null...
@endisset

@empty($records)
    // $records is "empty"...
@endempty
@auth('admin')
    // The user is authenticated...
@endauth

@guest('admin')
    // The user is not authenticated...
@endguest
@switch($i)
    @case(1)
        First case...
        @break

    @case(2)
        Second case...
        @break

    @default
        Default case...
@endswitch
@foreach ($users as $user)
    @if ($user->type == 1)
        @continue       # 结束此次循环
    @endif

    <li>{{ $user->name }}</li>

    @if ($user->number == 5)
        @break          # 跳出当前循环
    @endif
@endforeach

# 等同于
@foreach ($users as $user)
    @continue($user->type == 1)

    <li>{{ $user->name }}</li>

    @break($user->number == 5)
@endforeach

循环结构

当前循环是不是首次迭代,又或者当前循环是不是最后一次迭代:

@foreach ($users as $user)
    @if ($loop->first)
        This is the first iteration.
    @endif

    @if ($loop->last)
        This is the last iteration.
    @endif

    <p>This is user {{ $user->id }}</p>
@endforeach

# 嵌套结构还可以这么玩儿
@foreach ($users as $user)
    @foreach ($user->posts as $post)
        @if ($loop->parent->first)
            This is first iteration of the parent loop.
        @endif
    @endforeach
@endforeach
// 适用于 forelse
$loop->index       当前循环所迭代的索引,起始为 0$loop->iteration   当前迭代数,起始为 1$loop->remaining   循环中迭代剩余的数量。 // 不包含当前的这次
$loop->count       被迭代项的总数量。
$loop->firs        当前迭代是否是循环中的首次迭代。
$loop->last        当前迭代是否是循环中的最后一次迭代。
$loop->depth       当前循环的嵌套深度。
$loop->parent      当在嵌套的循环内时,可以访问到父循环中的 $loop 变量。

子视图

所有在父视图的可用变量在被引入的视图中都是可用的。

@include('shared.errors')

如果你想引入一个视图,而你又无法确认这个视图存在与否,你可以使用 @includeIf 指令:

@includeIf('view.name', ['some' => 'data'])
@includeWhen($boolean, 'view.name', ['some' => 'data'])
@includeFirst(['custom.admin', 'admin'], ['some' => 'data'])     // 谁先存在先加载谁

为集合数组渲染视图

// 子视图使用 key 变量作为当前迭代的键名。
// 注意:通过 @each 不会从父视图继承变量。 如果子需要这些变量,应该使用 @foreach 和 @include。
@each('view.name', $jobs, 'job')
@each('view.name', $jobs, 'job', 'view.empty')

// 自己做的例子
<!-- alert.blade.php -->
<div class="alert alert-danger">
    {{ $aa }}
</div>

<!-- other.blade.php -->
@each('alert', ['aaa','bbb'], 'aa')

/********** 输出 *********
<div class="alert alert-danger">
    aaa
</div>
<div class="alert alert-danger">
    bbb
</div>
************************/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值