How to use laravel blade dynamic templates with View::first

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.