Home Backend Development PHP Tutorial Detailed explanation of the use of Blade templates in Laravel

Detailed explanation of the use of Blade templates in Laravel

Sep 09, 2017 am 10:06 AM
blade laravel Detailed explanation

Blade is a simple and powerful template engine provided by laravel. The following article mainly introduces you to the relevant information about the use of Blade templates in the Laravel framework. The article introduces it in great detail through example code, which is very useful for your study or work. It has certain reference and learning value. Friends who need it can take a look below.

Introduction

Blade does not restrict you from using native PHP code in views like other popular PHP template engines. In fact, it Just compile the Blade view into native PHP code and cache it. The cache changes when the Blade view changes, which means Blade adds no compilation burden to your application. Blade view files use the .blade.php suffix and are generally stored in the resources/views directory.

1. Inheritance, fragments, placeholders, components, slots

1.1 Inheritance

1.1.1 Define parent template


Laravel/resources/views/base.blade.php
Copy after login

1.1.2 Child template inheritance

Path: Laravel/resources/views/child .blade.php


@extends('base')
Copy after login

1.2 Fragment

##1.2.1 Parent template definition fragment


@section('part')
// 中间内容即使一个片段
@show
Copy after login

1.2.2 Sub-template filling fragment


@section('part')
Copy after login

Fragment filling content



@endsection
Copy after login
Copy after login

1.3 Placeholder

1.3.1 Parent template placeholder:


@yield('title')
Copy after login

1.3.2 Subtemplate filling placeholder

First filling (text):



@section('title' , '填充的文本占位')
Copy after login

Second filling (text or html)



@section('title')
Copy after login

Filled placeholders


##

@endsection
Copy after login
Copy after login

1.4 Components, slots


1.4.1 Define components

Path: Laravel/resources/views/component.blade.php

<p class=&#39;component&#39;>
 <!-- $title,$content 变量实际上就是预定义的插槽 -->
 <p class=&#39;title&#39;>{{ $title }}</p>
 <p class=&#39;content&#39;>{{ $content }}</p>
</p>
Copy after login

1.4 .2 Using components

Path: Laravel/resources/views/test.blade.php

@component(&#39;component&#39;)
 @slot(&#39;title&#39;)
  组件标题
 @endsolt
 
 @slot(&#39;content&#39;)
  组件内容
 @endslot
@endcomponent
Copy after login

2. Data display

2.1 Escaped output


##
{{ $name }}
Copy after login

2.2 Unescaped output


{!! $name !!}
Copy after login

2.3 Original format output

The first type (not suitable for much):


@{{ name }}
Copy after login

The second type (suitable for large amounts):


@verbatim
{{ name }}
{{ sex }}
{{ age }}
@endverbatim
Copy after login


3. Process control


3.1 for


Note:

There is no $loop variable
  • There is no @empty
  • There is @break
  • # #有@continue

  • ##
    @for ($i = 0; $i < 10; ++$i)
     {{ $i }} <br />
    @endfor
    Copy after login
3.2 foreach


Note :

##There is $loop variable

    There is no @empty
  • ## There is @break
  • ##There is @continue

  • ##
    @foreach ($data as $k => $v)
     {{ $k }} <br />
    @endforeach
    Copy after login
  • 3.3 forelse


Note:


The $loop variable

is required There is @empty

  • There is @break

  • There is @continue

  • @foreach ($data as $k => $v)
     {{ $k }} <br />
    @empty
    Copy after login
  • The array has no data
@endforeach
Copy after login


4. Use native PHP



@php 
echo "使用原生 PHP";
@endphp
Copy after login
5. Containing subviews


Note

The included subview can reference all variables defined by the parent view.

You can pass additional data to the child view

  • Define the parent view parent.blade.php, and include the child view child.blade. php, and pass in additional data

  • /**
     * 父视图
     * 父视图拥有变量 $name = &#39;chenxuelong&#39;
     */
    
    <p class=&#39;parent&#39;>
     <p class=&#39;username&#39;>{{ $username }}</p>
     <p class=&#39;child&#39;>
      <!-- 包含子视图 -->
      @include(&#39;child&#39; , [
       &#39;other&#39; => &#39;额外数据&#39;
      ])
     </p>
    </p>
    
    /**
     * 子视图
     */
     <p class=&#39;username&#39;>{{ $username }}</p>
     <p class=&#39;other&#39;>{{ $other }}</p>
    Copy after login

    Summary

    The above is the detailed content of Detailed explanation of the use of Blade templates in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel - Artisan Commands Laravel - Artisan Commands Aug 27, 2024 am 10:51 AM

Laravel - Artisan Commands - Laravel 5.7 comes with new way of treating and testing new commands. It includes a new feature of testing artisan commands and the demonstration is mentioned below ?

Laravel - Artisan Console Laravel - Artisan Console Aug 27, 2024 am 10:51 AM

Laravel - Artisan Console - Laravel framework provides three primary tools for interaction through command-line namely: Artisan, Ticker and REPL. This chapter explains about Artisan in detail.

Laravel - Pagination Customizations Laravel - Pagination Customizations Aug 27, 2024 am 10:51 AM

Laravel - Pagination Customizations - Laravel includes a feature of pagination which helps a user or a developer to include a pagination feature. Laravel paginator is integrated with the query builder and Eloquent ORM. The paginate method automatical

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

See all articles