This article mainly introduces to you about how to use the View::first laravel Blade Dynamic template of the relevant information, the text through the sample code introduced in very detailed, for everyone to learn or use PHP has a certain reference learning value, the need for friends to come together to see it.
Objective
This article mainly introduce to you about View::first use laravel blade dynamic template of related content, share out for everyone reference study, the following words do not say more, come together to see the detailed introduction bar.
When creating a dynamic component or page, sometimes we want to show it when the custom template is present, otherwise the default template is displayed.
For example, when we create a page module, we usually need to give "about us" and "Contact Us" custom templates (such as display photos or contact forms), while "Our services" can use the default template.
We can judge whether a 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 function.
Use of View::first
view()->first()
Method allows us to put the following code
if (view ()->exists (' custom-template ')) {return view (' Custom-template ', $data);} return view (' Default-template ', $ data);
Replace with a more concise version:
Return view ()->first ( [' custom-template ', ' default-template '], $data);
An array must be passed to the first argument of the method, which will be used when the first one exists.
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 the facade version of this feature:
\view::first ($templates, $data)
The Blade method of this dynamic selection template was introduced in Laravel 5.5, making it more concise to deal with dynamic templates without the need for additional conditions to judge.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!