Laravel provides a blade template engine for rendering of views. PHP code can be used directly in blade, and blade will eventually be compiled into php and cached. It will only be recompiled after the blade file is modified, which can save overhead and improve application performance. The blade file is stored as a view file in the resource/views directory of laravel.
1. Define the template
Blade defines a template page like creating an html page, but it only takes up a placeholder through @section or @yield in the appropriate position. When other pages refer to the template page, the content can be filled to the placeholder position.
<html> <head> <title>@yield('title')</title> </head> <body> <header class="header"> @section('header') This is the head<br> @show </header> <div class="middle"> <aside class="aside"> This is the sidebar @yield('aside') </aside> <div class="content"> @section('content') This is the main content @show </div> </div> <footer class="footer"> This is the bottom @yield('footer') </footer> </body> </html>
Both section and yield are placeholders. The difference is reflected in the reference template. When using yield, the specified placeholder will be completely replaced. When using section, the content between @section()~@show can be retained through @parent.
If you need to introduce external js and css files in blade, you can use the absolute path relative to the public directory, such as introducing the built-in bootstrap, located in public/css/, you can <link rel="stylesheet" href="{{ asset('./css/')}}" rel="external nofollow" >
2. Quote template
Referring to a template first requires introducing the template you need to use through @extends(), and the template position is relative to the views directory. Then use @section()~@stop (note the difference from @section~@show when defining the template), fill the content you need to replace to the specified position of the template, for example, to fill the corresponding section of the header:
@extends('') {{--Introduce templatesviews/template/--}} @section('title') Login interface @stop @section('header') {{--Fill inheaderCorresponding placeholders--}} @parent {{--Keep the original content of the template--}} Head replacement content @stop
Introduce components: Introduce component templates through @component. For example, a common error prompt component alert is defined:
<div style="color: #ff5b5d;"> <h5>{{$title}}</h5> {{$slot}} </div>
Use this component in the page:
@component('') {{--Introducing componentsviews/template/--}} @slot('title') {{--Specify the alternative component$titleLocation--}} alerttitle @endslot alertComponent content @endcomponent
The content between @component~@endcomponent will automatically replace the component {{$slot}}. If you want to specify the location of the replacement, you can use @slot()~@endslot
Introduce subviews: If you want to introduce a blade subview in a page, you can use @include()
@include('')
The output variable in blade is {{$var}}, and the statements have been processed by PHP's htmlentities function to avoid XSS attacks. For example, when introducing a view in the controller, variable parameters are passed:
public static function showBlade(){ return view('',['var'=>'test']); }
Use this variable in blade
The variable is: {{isset($var)? $var: 'default'}}
Sometimes we hope that blade does not parse the text and output it as is. For example, we also use {{}} to wrap variables in vue. We do not want blade to compile it. At this time, you can use @:
Original text output: @{{ $var }}
3. Process control
blade provides a set of process control statements to control page rendering, making page rendering faster, and these control statements are very similar to PHP.
If judgment:
@if ($val >80) excellent @elseif ($val>60) Pass @else 不Pass @endif
cycle:
@for ($i = 0; $i < 10; $i++) The current value is {{ $i }} @endfor @foreach ($users as $user) <p>This is user {{ $user->id }}</p> @endforeach
Switch branch:
@switch($i) @case(1) First case... @break @case(2) Second case... @break @default Default case... @endswitch
Authentication: The @auth and @guest directives can be used to quickly determine whether the current user is logged in:
@auth // The user is logged in...@endauth @guest // The user is not logged in...@endguest
The above article Laravel's rendering view through the blade template engine is all the content I share with you. I hope you can give you a reference and I hope you can support me more.