Don’t Wash dishes studio – Hanxiao

The copyright belongs to the author, please indicate the source

Why use blade templates

  • Easy to understand, clear thinking
  • Convenient, can be directly used in the framework, you can intuitively observe the appearance of their own code
  • The processing of background data is convenient, and the parameter transfer between blade template and framework is very convenient, which can be said to be arbitrary
  • Complete documentation, basic problems can be checked
  • Suitable for single-player development, after the completion of the back-end implementation can be directly called in the Blade template, easy to write
  • You can use styles such as bootstrap to render pages to meet some basic beautification needs

limitations

  • Can only do some simple pages, complex pages or front and back end separation is more suitable
  • I can’t think of it. It really works! For a guy to write a small project with this thing is a real gem

Some insights and understanding of the use of templates

We’re not going to talk about how templates function, we’re just going to use them

  1. Design method: We can in accordance with the simplest of ideas to design your own blade templates, see page, for example, a personal blog, we design our navigation and footer almost will not change, so we can look this general independence of every page to generate a template, and then let the other pages inherit the independence of the template, It can be understood that other pages are based on this page to arrange

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

  3. Take the example given in the document:

  4. This is the simplest use, and we can extend this method so that we can return a simple variable or a complex variable.

  5. We can also call methods on a page, such as the framework’s built-in user system, which we can use directly in the templateAuth::check()

  6. In this way, the comment module of the article has been implemented, is not very simple!

  7. Of course, if you want to add some personal service, you can use itService injection

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

tip

  • The above two are a brief overview of the basic operations, but I’d like to share a few tips I’ve learned personally using blades
    • Lazy people writing
      • Want to write a template often logic is clear, we need to work under the directory creation time, such as using the appropriate directory structure and naming, etc., but the process often let us very pain, actually this is worth it, because to do so later can become very convenient), because was not a big project, I wasted my time on naming files and thinking about directory structures. So I combined the blade template’s if statement with parameter passing to come up with a way to write multiple pages in a blade file
      • The concrete implementation is as followsreturn view, we add one more parameter,routeParameter, and then check the value of route in the blade template file, returned by different methodsrouteThe values are different so that we can passifStatement to determine which view to display differently.So that’s the abstraction, the left hand side is the usual writing, and the right hand side is our lazy writing
    • Routes are combined with parameters
      • Take the simplest example is the blog post system, concrete implementation: we can set routing like this
      • Route::get('/article-{article_id}','ArticleController@showArticle');
      • Place the corresponding article links on the page like this<a href="{{url('/article/'.$article->id)}}">{{ $article->title }}</a>

summary

Blade templates collide with Laravel frames to create an unexpected spark, and there are many, many interesting uses for both of them that require our imagination. There are a lot of requirements and scenarios that do not necessarily require us to master how difficult technology, flexible use of existing tools can often solve a lot of problems.

PS: lazy writing only trial small projects and writing to play, is found to be killed by leaders or peers, I hope my examples can cause you some thinking, give you some inspirationCopy the code