Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the blade template of Laravel framework

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use the blade template of the Laravel framework. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Brief introduction

Blade is a simple and powerful template engine provided by Laravel. Compared with other well-known PHP template engines, Blade does not restrict you to use PHP code in the view. All Blade views are compiled and cached into normal PHP code until they are changed. This means that Blade basically does not impose a burden on your application.

The Blade view file uses .blade.php as the extension and is usually saved in the resources/views folder.

Why use blade templates

Easy to understand and clear thinking

Convenient, can be used directly in the framework, you can intuitively observe the appearance of the code you write

It is convenient to deal with the background data, and the parameter transfer between the blade template and the framework is so convenient that it can be said to be arbitrary.

The documents are complete, and you can basically find out if there are any problems.

It is suitable for individual development. After completing the backend implementation, it can be called directly in the blade template, and it is easy to write.

Can use bootstrap and other styles to render the page, you can achieve some basic beautification requirements

Limitation

Can only do some simple pages, complex pages or front-end separation is more suitable

I can't think of it. It really works! It's really an artifact to write a small project as a rookie.

Some experience and understanding of the use of templates

We won't talk about the functional implementation of the template, we'll just talk about using

1. Design method: we can design our own blade template according to the simplest idea, that is, look at the page. For example, if we design a personal blog, our navigation bar and footer will hardly change, so we can first separate the common place of each page to generate a template, and then let other pages inherit this independent template. It can be understood that all other pages are based on this page.

2, data display: the point is, the parameter transfer of the blade template is really convenient, when we look at the document, we can see that the background can attach parameters to the page when return view, and then we can directly call the data in the form of variables in the page.

3. For example, the example given in the document:

4. This is the simplest usage. We can extend this method. We can return either a simple variable or a complex variable.

5. We can also call some methods on a page, such as the user system that comes with the framework, and we can use the method Auth::check () directly in the template.

6. In this way, the comment module of the article is realized, which is not very simple!

7. Of course, if you want to add some personal services, you can use service injection (inject)

8. For example, if I want to select the category to which the article belongs when I create an article, I can inject the category service into it.

Tips

The above two are a brief overview of the basic operations. Let me share a few tips I have personally summed up when using blade.

Lazy people's writing

Writing a logical template often requires us to make some effort when creating a directory, such as using the appropriate directory structure and naming, etc., but this process is often painful (in fact, it is worth it. Because it will be very convenient for later maintenance), because it is not a very big project, but I waste my time on naming files and conceiving the directory structure. So I combined the if statement and parameter passing of the blade template to come up with a way to write multiple pages in one blade file.

The specific implementation is as follows: in return view, we add one more parameter, route parameter, and then judge the value of route in the blade template file. Different methods return different route values, so we can judge through the if statement and choose to display different views.

Abstract, that's it, on the left is the common writing, and on the right is our lazy person's writing.

Combination of routing and parameters

The simplest example is the blog article system, which is implemented: we can set up routing like this

Route::get ('/ article- {article_id}', 'ArticleController@showArticle')

Arrange the corresponding article links {{$article- > title}} in the page like this

On "how to use the blade template of the Laravel framework" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report