Blade Templates

 For clear code view purposes to present it on the blog I didn't use templates in my project but in real world development to avoid code repetition and make the code look tidy, blade template are used in every laravel based project. I will now demonstrate how blade templates work creating a footer which will be visible at almost every page of my website. 

Footer Template view:

Implementing that template in other views:

@include method allows to include a blade view from within another view. All variables that are available to the parent view will be made available to the included view.

Now every page will have the same footer:

To create whole layouts for our pages we can also use @yield and @section methods

Head Layout View:
 

As an example (I will change it back in my code after showing this example, this is just for explanation purposes) in a layout file we put the HTML tag and all needed head elements, and links we will use in every page. With @yield we then say that the content inside <body> tags will be stated in view that will be extending that layout. We will do that using @extend method and set the body content using @section method in other views, for this example in Post with comments view. 

  

(I know I shouldn't put whole navbar in the section and etc, but this is just an example to describe how yielding works in Laravel). 

As we can see the page looks exactly the same but our Post view file looks more tidy and contains less lines of code:

These are just examples of a small pieces of HTML code, but we can do that with whole page's content and save so much space and avoid repeating multiple code lines in every view file. Blade templates, layouts and components are very powerful tools that should always be used in Laravel based projects.

Komentarze