For details about how to use the dynamic template of Laravel Blade through View: first, laravelblade

Source: Internet
Author: User

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.

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.