Detailed explanation of the usage of Blade template in Laravel

*文
Release: 2023-03-19 08:40:02
Original
1557 people have browsed it

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. I hope to be helpful.

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 Child 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 Child template fill placeholder

The first type of filling (text):

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

The second type of filling (text or html)

@section('title')
Copy after login

Filled placeholder

@endsection
Copy after login
Copy after login

1.4 Components, slots

1.4.1 Define components

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

{{ $title }}

{{ $content }}

Copy after login

1.4.2 Use component

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

@component('component')
 @slot('title')
  组件标题
 @endsolt
 
 @slot('content')
  组件内容
 @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

First type (suitable for Small quantity):

@{{ name }}
Copy after login

The second type (suitable for large quantity):

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

3. Process control

3.1 for

Note:

  • There is no $loop variable

  • No @empty

  • Yes @break

  • Yes @continue

@for ($i = 0; $i < 10; ++$i)
 {{ $i }} 
@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 }} 
@endforeach
Copy after login

3.3 forelse

##Note:

  • There is $loop variable

  • There must be @empty

  • There is @break

  • Yes@continue

  • @foreach ($data as $k => $v)
     {{ $k }} 
    @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. Contain 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 = 'chenxuelong'
 */

{{ $username }}

@include('child' , [ 'other' => '额外数据' ])

/** * 子视图 */

{{ $username }}

{{ $other }}

Copy after login

Related recommendations:

Explore how Laravel’s middleware is implemented

Laravel optimized split routing file

laravel writing APP interface (API)

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!