Blade is a simple and powerful template engine provided by Laravel, the following article mainly introduces the Laravel framework of Blade template usage Related Materials, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, The friends who need to take a look below. We hope to help you.
Brief introduction
Blade It doesn't limit you to using native PHP code in your view like any other popular PHP template engine, in fact it compiles Blade views into native PHP code and caches them. The cache changes when the Blade view changes, which means that Blade does not add the burden of compiling your app. The Blade view file uses the. blade.php suffix, which is typically stored in the Resources/views directory.
1. Inheritance, fragments, placeholders, components, slots
1.1 Inheritance
1.1.1 Defining a parent template
laravel/resources/views/base.blade.php
1.1.2 Child Template Inheritance
Path: laravel/resources/views/child.blade.php
@extends (' base ')
1.2 Fragments
1.2.1 Parent Template Definition Fragment
@section (' part ')//intermediate content even if a fragment @show
1.2.2 Child template Fill Fragment
@section (' part ')
Fragment Fill Content
@endsection
1.3 Placeholder
1.3.1 Parent Template placeholder:
@yield (' title ')
1.3.2 Sub-template fill placeholder
First Fill (text):
@section (' title ', ' filled-in text placeholder ')
Second fill (text or HTML)
@section (' title ')
Placeholder for fill
@endsection
1.4 Components, Slots
1.4.1 Defining components
Path: laravel/resources/views/component.blade.php
<p class= ' component ' > <!--$title, $content variables are actually predefined slots--<p class= ' title ' >{{$title}}</p> & Lt;p class= ' content ' >{{$content}}</p></p>
1.4.2 Using Components
Path: laravel/resources/views/test.blade.php
@component (' component ') @slot (' title ') component title @endsolt @slot (' content ') component content @endslot @endcomponent
2. Data display
2.1 Escape output
{{$name}}
2.2 Output not escaped
{!! $name!}
2.3 Original Format output
The first one (not much for the amount):
@{{name}}
The second kind (suitable quantity is many):
@verbatim {{Name}}{{sex}}{{Age}} @endverbatim
3. Process Control
3.1 for
Attention:
No $loop variables
No @empty
Have @break
Have @continue
@for ($i = 0; $i < + + $i) {{$i}} <br/> @endfor
3.2 foreach
Attention:
@foreach ($data as $k = = $v) {{$k}} <br/> @endforeach
3.3 Forelse
Attention:
@foreach ($data as $k = = $v) {{$k}} <br/> @empty
Array has no data
@endforeach
4. Using native PHP
@php echo "Using native PHP"; @endphp
5. Include child views
Attention
Define the parent view parent.blade.php, and include the child View child.blade.php, and pass in additional data
/** * Parent View * Parent view has variable $name = ' chenxuelong ' */<p class= ' parent ' > <p class= ' username ' >{{$username}}</p> & Lt;p class= ' child ' > <!--contains sub-views -@include (' Children ', [' other ' + ' extra data ' ]) </p> </p>/** * Sub View */<p class= ' username ' >{{$username}}</p> <p class= ' other ' >{{$other}}</p>
Related recommendations:
Explore how the middleware of Laravel is implemented
Laravel Optimized Split routing file
Laravel writing the App interface (API)