How to use Laravel Blade's dynamic templates via View::first

不言
Release: 2023-04-01 17:12:01
Original
1188 people have browsed it

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);
Copy after login

with a more concise version:

return view()->first(
  ['custom-template', 'default-template'], $data
);
Copy after login

An array must be passed to the first parameter of this method. When the first one exists, it will use it.

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);
Copy after login

In addition, you can also pass this feature of the Facade version:

\View::first($templates, $data)
Copy after login

This Blade method of dynamically selecting templates was introduced in Laravel 5.5, which makes processing dynamic templates more concise and does not require additional conditional judgments.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template