Blade 模板引擎
模板继承
定义布局:
| |
| <html> |
| <head> |
| <title>App Name - @yield('title')</title> |
| </head> |
| <body> |
| @section('sidebar') |
| This is the master sidebar. |
| @show |
| <div class="container"> |
| @yield('content') |
| </div> |
| </body> |
| </html> |
继承布局:
| |
| @extends('layouts.app') |
| |
| @section('title', 'Page Title') |
| |
| @section('sidebar') |
| @parent |
| <p>This is appended to the master sidebar.</p> |
| @endsection |
| |
| @section('content') |
| <p>This is my body content.</p> |
| @endsection |
数据显示
注:Blade 的 {{}} 语句已经经过 PHP 的 htmlentities 函数处理以避免 XSS 攻击。
| Hello, {{ $name }}. |
| |
| The current UNIX timestamp is {{ time() }}. |
输出存在的数据, 两种方式都可以:
| {{ isset($name) ? $name : 'Default' }} |
| |
| {{ $name or 'Default' }} |
显示原生数据:
流程控制
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 |
循环:
| @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 |
使用循环的时候还可以结束循环或跳出当前迭代:
| @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 |
$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 |
$loop
变量还提供了其他一些有用的属性:
属性 | 描述 |
$loop->index | 当前循环迭代索引 (从0开始) |
$loop->iteration | 当前循环迭代 (从1开始) |
$loop->remaining | 当前循环剩余的迭代 |
$loop->count | 迭代数组元素的总数量 |
$loop->first | 是否是当前循环的第一个迭代 |
$loop->last | 是否是当前循环的最后一个迭代 |
$loop->depth | 当前循环的嵌套层级 |
$loop->parent | 嵌套循环中的父级循环变量 |
模板注释
| {{-- This comment will not be present in the rendered HTML --}} |
嵌入 PHP 代码
文章来源于本人博客,发布于 2018-06-02,原文链接:https://imlht.com/archives/156/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?