Laravel 5.5 Blade

Blade(模板)

// app.blade.php
    @section('sidebar')
        这里是侧边栏
    @show
    <div class="container">
         @yield('content')
     </div>
// child.blade.php
    @extends('layouts.app')
    @section('title', 'Laravel学院')

    @section('sidebar')
        @parent
        <p>Laravel学院致力于提供优质Laravel中文学习资源</p>
    @endsection

    @section('content')
        <p>这里是主体内容,完善中...</p>
    @endsection
// 流程控制
    (如果)
        @if (count($records) === 1)
            I have one record!
        @elseif (count($records) > 1)
            I have multiple records!
        @else
            I don't have any records!
        @endif
    
    (除非)
        @unless (Auth::check())
            You are not signed in.
        @endunless
    
    (是否存在)
        @isset($records)
            // $records is defined and is not null...
        @endisset
    
    (是否为空)
        @empty($records)
            // $records is "empty"...
        @endempty
    
    (认证指令)
        @auth
            // 用户已登录...
        @endauth

        @guest
            // The user is not authenticated...
        @else
            //TODO
        @endguest
    
    (Switch 语句)
        @switch($i)
            @case(1)
                First case...
                @break

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

            @default
                Default case...
        @endswitch
    
    (循环)
        @for ($i = 0; $i < 10; $i++)
            The current value is {{ $i }}
        @endfor

        @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

        @forelse ($users as $user)
            <li>{{ $user->name }}</li>
        @empty
            <p>No users</p>
        @endforelse

        @while (true)
            <p>I'm looping forever.</p>
        @endwhile
// $loop变量
    * 在循环的时候,可以在循环体中使用 $loop 变量,该变量提供了一些有用的信息,比如当前循环索引,以及当前循环是不是第一个或最后一个迭代
        
        @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
    
    * 如果你身处嵌套循环,可以通过 $loop 变量的 parent 属性访问父级循环:
        @foreach ($users as $user)
            @foreach ($user->posts as $post)
                @if ($loop->parent->first)
                    This is first iteration of the parent loop.
                @endif
            @endforeach
        @endforeach
// php 源码嵌入
    @php
        // TODO
    @endphp
// 包含子视图
    @include('shared.errors')
    @include('view.name', ['some' => 'data'])
    @includeIf('view.name', ['some' => 'data'])
    @includeWhen($boolean, 'view.name', ['some' => 'data'])
    @includeFirst(['custom.admin', 'admin'], ['some' => 'data'])

// 其他
    @component('alert')
        <strong>Whoops!</strong> Something went wrong!
    @endcomponent

    {{ $name or 'Default' }}
    {!! $name !!}
    @json($array); json化字符串
    @lang('messages.welcome') 获取翻译字符串

    @can('update', $post)
        <!-- 当前用户可以更新文章 -->
    @elsecan('create', App\Post::class)
        <!-- 当前用户可以创建新文章 -->
    @endcan

    @cannot('update', $post)
        <!-- 当前用户不能更新文章 -->
    @elsecannot('create', App\Post::class)
        <!-- 当前用户不能创建新文章 -->
    @endcannot


// 判断视图是否存在
     if (View::exists('emails.customer')) {
         // return ture or false
     }

 // 在视图间共享数据
     有时候,我们需要在所有视图之间共享数据片段,这时候可以使用视图门面的 share 方法,通常,需要在某个服务提供者的 boot 方法中调用 share 方法,你可以将其添加到 AppServiceProvider 或生成独立的服务提供者来存放这段代码逻辑
     public function boot()
     {
         View::share('key', 'value');
     }

猜你喜欢

转载自blog.csdn.net/qq_37910492/article/details/84544131