Lavarel5.1入门-Blade模板引擎

  1. Blade视图文件使用.blade.php文件扩展并存放在resources/views目录下。
  2. 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

<h3>@yield('firstStr')</h3>
<h3>{{ time() }}</h3>
<h3>{{ isset($name) ? $name : 'Default' }}</h3>
<h3>
            @if (count($records) === 1)
                I have one record!
            @elseif (count($records) > 1)
                I have multiple records!
            @else
                I don't have any records!
            @endif
</h3>
<div class="container">
            @yield('content')
 </div>

child.blade.php

@extends('layouts.master')
@section('firstStr')
 {{ $firstStr }}
@endsection
@section('content')
    <p>This is my body content.</p>
    <h3>
        @inject('metrics', 'App\Services\Hello')
        <div>
        Monthly Revenue: {{ $metrics->haha() }}.
    </div>
    </h3>
@endsection

App/Services/Hello.php

<?php
namespace App\Services;
class Hello
{  
    public function haha()
    {
       return "Services Hello haha";
    }
}
  1. 循环
//
@for ($i = 0; $i < 10; $i++)
    The current value is {{ $i }}
@endfor
//
@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 (true)
    <p>I'm looping forever.</p>
@endwhile
  1. 注释
{{-- This comment will not be present in the rendered HTML --}}
  1. 子视图@include
  2. 服务注入

参考

Blade模板引擎

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容