Lavarel5.1入門-Bladeテンプレートエンジン

2003 ワード

  • Bladeビューファイルが使用する.blade.phpファイルはresources/viewsディレクトリの下に拡張され、保存されます.
  • routes.php
  • Route::get('/child', function () {
        $records=['a','b','c'];
        return view('layouts/child',[
        'firstStr'=>'hello child h3 firstStr',
        'name'=>'name hello',
        'records'=>$records
    ]);
    });
    

    master.blade.php

    @yield('firstStr')

    {{ time() }}

    {{ isset($name) ? $name : 'Default' }}

    @if (count($records) === 1) I have one record! @elseif (count($records) > 1) I have multiple records! @else I don't have any records! @endif

    @yield('content')

    child.blade.php
    @extends('layouts.master')
    @section('firstStr')
     {{ $firstStr }}
    @endsection
    @section('content')
        

    This is my body content.

    @inject('metrics', 'App\Services\Hello')
    Monthly Revenue: {{ $metrics->haha() }}.

    @endsection

    App/Services/Hello.php
  • サイクル
  • //
    @for ($i = 0; $i < 10; $i++)
        The current value is {{ $i }}
    @endfor
    //
    @foreach ($users as $user)
        

    This is user {{ $user->id }}

    @endforeach // @forelse ($users as $user)
  • {{ $user->name }}
  • @empty

    No users

    @endforelse // @while (true)

    I'm looping forever.

    @endwhile
  • 注記
  • {{-- This comment will not be present in the rendered HTML --}}
    
  • サブビュー@include
  • サービス注入
  • リファレンス
    Bladeテンプレートエンジン