Laravel Framework Blade Template Beginner's Tutorial

Source: Internet
Author: User
Tags php template
Blade is a simple and powerful template engine provided by Laravel. The following article mainly introduces you to the Laravel framework of the blade template Beginner's introductory tutorial and some of the use of tips, the article through the sample code introduced in very detailed, the need for friends can refer to.

Brief introduction

Blade is a simple and powerful template engine provided by Laravel. Compared to other well-known PHP template engines, Blade does not restrict you from having to use PHP code in your views. All Blade views will be compiled and cached into normal PHP code until they are changed. This means that Blade does not generate 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 to use the blade template

    • Easy to understand and clear-minded

    • Convenient, directly can be used in the framework, you can visually observe the way you write code

    • The processing of the background data is convenient, the parameter transfer between blade template and frame is too convenient, can be said to be arbitrary

    • The document is complete, have the question basically can find out

    • Suitable for single-person development, after the completion of the implementation of the backend can be directly in the blade template call, write up handy

    • You can use bootstrap and other styles to render the page, you can achieve some basic landscaping needs

Limitations

    • Can only do a few simple pages, complex pages or front-end separation more suitable

    • Can't think of it, it is really easy to use Ah! As a little white write a small project with this thing is really an artifact

Some experience and understanding of the use of templates

We're not going to talk about the function implementation of the template, we only talk about using

1, Design method: We can follow the simplest way to design their own blade template, that is, to look at the page, for example, we design a personal blog, our navigation bar and footer almost will not change, so we can first this each page is common place to create a template, Then let the other pages inherit this independent template, it can be understood that the other pages are based on this page to decorate

2, the data show: The key to, Blade template parameter transfer is very convenient, in looking at the document when we are not difficult to see, backstage in the return view when you can give the page parameters, and then we can be in the form of variables in the page directly call the data.

3, for example, the example given in the document:

4, this is the simplest use, we can expand the method, we can return a simple variable, can also be a complex variable.

5, we can also call some methods on a page, for example, the framework of the user system, we can use the template directly in the method Auth::check()

6, so that the comments on the article module has been implemented, is not very simple!

7, of course, if you want to join a number of personal services, you can use service injection (inject)

8, for example, when I create an article to select the category of the article, you can inject the category service in

Little Tricks

The above two are a brief overview of basic operations, let me share some of my personal tips when using blade

The Lazy man's wording

To write a clear and logical template often requires us to work on creating a directory, such as the use of the appropriate directory structure and naming, etc., but this process often makes us very painful (in fact, it is worth it, because it is very convenient for later maintenance), because it is not a very large project, I wasted my time on naming files and thinking about the structure of the directory. So I combine the IF statements and parameters of the blade template and come up with a notation that can be done on multiple pages in a blade file

The specific implementation is as follows: In return view, we add a parameter, the route parameter, and then in the blade template file to determine the value of the route, different methods return the route value is different, so that we can be judged by the IF statement, so that we choose to display different views.

Abstract some is this, the left is commonly used, the right is our lazy people writing

Combination of Routing and parameters

The simplest example is the blog post system, specifically: we can set the route

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

Arrange the corresponding article link in this page<a href="{{url('/article/'.$article->id)}}" rel="external nofollow" >{{ $article->title }}</a>

Summary

Blade template collision with the laravel frame can produce an intentional spark, and there must be a lot of interesting uses for both of them, which requires our imagination. There are a lot of requirements and scenarios do not necessarily require us to grasp how difficult the technology, flexible use of the tools we have in hand can often solve a lot of problems.

PS: Lazy Writing only try small items and write to play when the leader or companion found to be killed

Summarize

Articles you may be interested in:

PHP generated thumbnail quality poor solution code explanation

PHP implementation of the stack data structure example explained

Laravel tips for Query Builder How to add chain call method

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.