This article mainly for you to share the introduction of 5 very useful laravel blade instructions, the text through the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, need friends to see it together
Brief introduction
Blade is a very simple and powerful template engine provided by Laravel, unlike other popular PHP template engines, Blade does not constrain your use of PHP native code in the view. All blade views are compiled into native PHP code and cached until modified, which means that blade is basically 0 overhead for the performance of the application. The blade view file is extended using the. blade.php file and is stored in the Resources/views directory.
Next I'll take you to the next five Laravel Blade instructions that will give you a powerful solution to a particular problem. If you're new to Laravel, these tips will take you to the convenience and efficiency of the Laravel Blade template engine.
Talk less, let's get started.
1. Detect whether the user is certified
You can verify that the user is authenticated by verifying that they are empty:
@if (Auth ()->user ())//user authenticated @endif
However, Laravel's own Blade commands can be more concise to achieve the same functionality:
@auth//user authenticated @endauth
2. Detect if the user is a guest
Contrary to authentication, we can use the guest () method of the Auth helper function to detect whether a user is a guest:
@if (Auth ()->guest ())//user not certified @endif
However, Laravel also provides @guest commands for this:
@guest//user not certified @endguest
We can also use the Else statement to combine the two commands:
@guest//user not certified @else//user authenticated @endguest
3. If the first view exists, it is introduced, otherwise a second
Building a multi-theme site may have a file that is introduced if it exists, or if it introduces another need, you can simply use conditional judgment to implement:
@if (View ()->exists (' First-view-name ') @include (' First-view-name ') @else @include (' second-view-name ') @endif
However, there is a more concise and intuitive command to do this:
@includeFirst ([' First-view-name ', ' second-view-name ']);
4. Introduce views based on conditions
When you want to add something to a certain logical basis (such as a user who has been authenticated), it is very useful to introduce a view based on the criteria.
You can use @if conditions to write this:
@if ($post->hascomments ()) @include (' posts.comments ') @endif
We can do this with just one line of command @includeWhen:
@includeWhen ($post->hascomments (), ' posts.comments ');
5. Introduction of an existing view
If you have a custom theme system or you need to create a Blade view dynamically, then checking whether the file exists is something you have to do.
You can call the Exists method on the Auxiliary Function view ():
@if (View ()->exists (' View-name ')) @include (' View-name ') @endif
You can also use the Blade command Includeif to handle:
@includeIf (' View-name ')
You can use the Blade official documentation to learn more useful tips for optimizing the front-end templates in your Laravel project.
Re-construct happy!