前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >最为常用的Laravel操作(3)-模板

最为常用的Laravel操作(3)-模板

原创
作者头像
仁扬
发布2023-07-01 21:38:11
1430
发布2023-07-01 21:38:11
举报
文章被收录于专栏:仁扬笔记仁扬笔记

Blade 模板引擎

模板继承

定义布局:

代码语言:html
复制
<!-- 存放在 resources/views/layouts/app.blade.php -->
<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>

继承布局:

代码语言:html
复制
<!-- 存放在 resources/views/child.blade.php -->
@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 攻击。

代码语言:html
复制
Hello, {{ $name }}.

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

输出存在的数据, 两种方式都可以:

代码语言:html
复制
{{ isset($name) ? $name : 'Default' }}

{{ $name or 'Default' }}

显示原生数据:

代码语言:html
复制
Hello, {!! $name !!}.
流程控制

if 语句:

代码语言:html
复制
@if (count($records) === 1)
    I have one record!
@elseif (count($records) > 1)
    I have multiple records!
@else
    I don't have any records!
@endif
代码语言:html
复制
@unless (Auth::check())
    You are not signed in.
@endunless

循环:

代码语言:html
复制
@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

使用循环的时候还可以结束循环或跳出当前迭代:

代码语言:html
复制
@foreach ($users as $user)
    @if ($user->type == 1)
        @continue
    @endif

    <li>{{ $user->name }}</li>

    @if ($user->number == 5)
        @break
    @endif
@endforeach

还可以使用指令声明来引入条件:

代码语言:html
复制
@foreach ($users as $user)
    @continue($user->type == 1)

        <li>{{ $user->name }}</li>

    @break($user->number == 5)
@endforeach
$loop 变量

在循环的时候, 可以在循环体中使用 $loop 变量, 该变量提供了一些有用的信息, 比如当前循环索引, 以及当前循环是不是第一个或最后一个迭代:

代码语言:html
复制
@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 属性访问父级循环:

代码语言:html
复制
@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

嵌套循环中的父级循环变量

模板注释
代码语言:html
复制
{{-- This comment will not be present in the rendered HTML --}}

嵌入 PHP 代码

代码语言:html
复制
@php
    //
@endphp

文章来源于本人博客,发布于 2018-06-02,原文链接:https://imlht.com/archives/156/

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Blade 模板引擎
    • 模板继承
      • 数据显示
        • 流程控制
          • $loop 变量
            • 模板注释
              • 嵌入 PHP 代码
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com