For details about how to use the dynamic template of Laravel Blade through View: first, laravelblade
Preface
This article mainly introduces the View: first uses the Laravel Blade dynamic template for your reference, let's take a look at the detailed introduction.
When creating a dynamic component or page, we sometimes want to display a custom template when it exists. Otherwise, the default template is displayed.
For example, when we create a page module, we usually need to customize templates (such as displaying photos or contact forms) for "About Us" and "Contact Us ), the default template can be used for "our services.
We can use a series of if statements orview()->exists() To determine whether a custom template exists. However, Laravel 5.5 brings us a more elegant way to implement this function.
View: Use of first
view()->first() The following code
if (view()->exists('custom-template')) { return view('custom-template', $data);} return view('default-template', $data);
Replace it with a simpler version:
return view()->first( ['custom-template', 'default-template'], $data);
You must pass an array to the first parameter of this method. When the first parameter exists, it will be used.
Of course, you can pass any number of templates, and you can even use dynamic names:
return view()->first([ "pages/{$page->slug}", "pages/category-{$page->category->slug}", "pages/default-template"], $data);
In addition, you can also use this feature of the Facade version:
\View::first($templates, $data)
The Blade method of this dynamic selection template is introduced in Laravel 5.5, making it easier to process dynamic templates without additional conditions.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.