Blade 控制語法結(jié)構(gòu)

2018-02-24 15:52 更新

在 Blade 視圖中打印(Echoing)數(shù)據(jù)

Hello, {{ $name }}.

The current UNIX timestamp is {{ time() }}.

檢查數(shù)據(jù)是否存在后再打印數(shù)據(jù)

有時(shí)候您想要打印一個(gè)變量,但您不確定這個(gè)變量是否存在,通常情況下,您會(huì)想要這樣寫::

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

然而,除了寫這種三元運(yùn)算符語法之外,Blade 讓您可以使用下面這種更簡便的語法:

{{ $name or 'Default' }}

使用花括號(hào)顯示文字

如果您需要顯示的一個(gè)字符串剛好被花括號(hào)包起來,您可以在花括號(hào)之前加上 @ 符號(hào)前綴來跳出 Blade 引擎的解析:

@{{ This will not be processed by Blade }}

如果您不想數(shù)據(jù)被轉(zhuǎn)義, 也可以使用如下語法:

Hello, {!! $name !!}.

特別注意: 在您的應(yīng)用程序打印用戶所提供的內(nèi)容時(shí)要非常小心。請(qǐng)記得永遠(yuǎn)使用雙重花括號(hào)來轉(zhuǎn)義內(nèi)容中的 HTML 實(shí)體字符串。

If 聲明

@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

循環(huán)

@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

加載子視圖

@include('view.name')

您也可以通過傳入數(shù)組的形式將數(shù)據(jù)傳遞給加載的子視圖:

@include('view.name', ['some' => 'data'])

重寫區(qū)塊

如果想要重寫掉前面區(qū)塊中的內(nèi)容,您可以使用 overwrite 聲明:

@extends('list.item.container')
@section('list.item.content')
    <p>This is an item of type {{ $item->type }}</p>
@overwrite

顯示語言行

@lang('language.line')
@choice('language.line', 1)

注釋

{{-- This comment will not be in the rendered HTML --}}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)