首页后端开发JAVA最为常用的Laravel操作(3)-模板

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

时间2023-10-19 01:35:02发布访客分类JAVA浏览1326
导读:Blade 模板引擎模板继承定义布局:<!-- 存放在 resources/views/layouts/app.blade.php --> <html> <head> <ti...

Blade 模板引擎

模板继承

定义布局:

!-- 存放在 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>
    

继承布局:

!-- 存放在 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 攻击。

Hello, {
{
 $name }
}
.

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

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

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


{
{
 $name or 'Default' }
}

显示原生数据:

Hello, {
!! $name !!}
    .

流程控制

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 代码

@php
    //
@endphp

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

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 最为常用的Laravel操作(3)-模板
本文地址: https://pptw.com/jishu/500899.html
正确处理 CSV 文件的引号和逗号 Golang 中文转拼音

游客 回复需填写必要信息