This article mainly introduces you to the relevant information on how to use Laravel Blade's dynamic template through View::first. The article introduces it in detail through the example code, which has certain reference learning value for everyone to learn or use PHP. Friends who need it can come and take a look.
Preface
This article mainly introduces the relevant content about View::first using Laravel Blade dynamic template, and shares it for your reference. Learning, I won’t say much more below, let’s take a look at the detailed introduction.
When creating dynamic components or pages, sometimes we want to display a custom template when it exists, otherwise display the default template.
For example, when we create a page module, we usually need to customize templates for "About Us" and "Contact Us" (such as displaying photos or contact forms), while "Our Services" can use the default template.
We can judge whether the custom template exists through a series of if judgments or use view()->exists()
. However, Laravel 5.5 brings us a more Elegant way to implement this functionality.
View::first can be used using the
##view()->first() method. Let us replace the following code
if (view()->exists('custom-template')) { return view('custom-template', $data); } return view('default-template', $data);
return view()->first( ['custom-template', 'default-template'], $data );
return view()->first([ "pages/{$page->slug}", "pages/category-{$page->category->slug}", "pages/default-template" ], $data);
\View::first($templates, $data)
Using PHP’s scope resolution operator (::)
For Laravel framework template loading And the function of assigning variables and simple routing
The above is the detailed content of How to use Laravel Blade's dynamic templates via View::first. For more information, please follow other related articles on the PHP Chinese website!