作者:南丞 时间:2017-12-25 01:00
1.模板继承
<html lang="en">
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
@section('head')
<link rel="stylesheet" href="style.css" />
@show
</head>
<body>
@yield('body')
@section('message')
@parent
<p>parent message.</p>
@show
</body>
</html>
<!-- app/views/home.blade.php -->
@extends('layouts.base')
@section('head')
<link rel="stylesheet" href="another.css" />
@endsection
@section('body')
<h1>Hurray!</h1>
<p>We have a template!</p>
@stop
@section('message')
@parent
<p>Fourth</p>
@stop
//直接输出变量
{ { $name } }
//输出带有html的字符串并解析
{ !! $links !! }
//原样输出
@{ { name } } //输出的结果是 { { name } }
注意:Blade { { } } 声明中的内容是自动通过 PHP 的 htmlentities 方法过滤的
{ { isset($name) ? $name : 'Default' } }
{ { $name or 'Default' } }
@if ($something == '1')
<p>1</p>
@elseif ($something == '2')
<p>2</p>
@else
<p>3</p>
@endif
@unless (true|false)
You are not signed in.
@endunless
//@hasSection 指令来判断提供给布局的挂件是否包含了内容
@hasSection('demo')
dd
@else
App Name
@endif
Blade 也提供了一些简单指令来支持 PHP 的循环结构。
@for ($i = 0; $i < 10; $i++)
The current value is
@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
Blade 也提供了终止迭代或取消当前迭代的指令:
@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></li>
@break($user->number == 5)
@endforeach
{ {-- This is a pretty, and secret Blade comment. --} }
@include('view.name')
@include('view.name', ['some' => 'data'])
@each('view.name', $jobs, 'job')
//@each 第一个参数是数组或集合中每个元素需要被渲染的视图名称。
//@each 第二个参数是一个数组或集合,被用来提供迭代。
//@each 第三个参数是要分配给当前视图的变量名
//@each 第四个参数到 @each 指令。如果所提供的数组是空数组的话,该参数所提供的视图将会被引入
Blade 已有的基础模板上加入新的内容
@push('scripts')
<script src="/example.js"></script>
@endpush
//使用这个
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
{ !! join(', ', $post->tagLinks()) !! }
{ { $time|date('Ymd') } }