In this article, I will discuss how you should use blade layout files, Blade is driven by template inheritance and sections. It really is a great way in pulling certain layouts/content into a view. This makes sure you are not copying & pasting the same code into multiple views. Imagine you have to make one change in the source code, do you really want to go through every single view and update the source code? No, we don’t. That is not the best practice.
CREATING A LAYOUT FILE
Head over into your code editor and go into /resources/views/ and create a new file called layout.blade.php. Remember to add the word blade into the filename, otherwise, it won’t work.
Once you’ve created the layout file, copy and paste the HTML from the welcome.blade.php view and into the layout file. The HTML that we will move over is shown below;
The HTML that has been added to the layout.blade.php file
Once you’ve done that, we need to add in the following code into the layout file;
Here we are telling Laravel to render the content. Go back to welcome.blade.php and delete everything that is in it. For now, we will add this bit of code in the view file.
We are telling Laravel to look into the views folder and locate the layout blade file that we created, whatever is inside that file will be rendered on the homepage. I’ve added in two links, one for the homepage, the other to the about page. This is how it will show on the homepage.
If you wish to do so, you could even create a layout direct inside the views folder. If you go down this route, no pun intended, then you would have to reference the layout file using the following;
Notice that we have used the ‘.’ to separate the directories. You could replace the period with a ‘/’ but I think it looks better visually with the period. For now, I’m going to keep the layout file where it is.
We now need to register a section, further up, we added the yield code into the template, but there is no section for it, so nothing will be rendered. To register a section we need to add the following to our template file;
@section(‘content’) <p>This is the content.</p> @endsection
Refresh the homepage and it’ll show you the content you have added. Remember, we need to use the blade syntax here. For every section you create, you must remember to add
@endsectionotherwise you’ll run into problems.
Remember, wherever you add the yield content snippet of code, Laravel will render the content there. So please, make sure you add it to a sensible place, for example, below the navigation and not above it.
TITLE TAGS — HOW CAN WE SET THIS?
If you take a look at the tab in your browser, you will notice that the page title is currently showing the URL. Now, that looks messy, doesn't it? It’s not really SEO friendly — so we need to get that changed. Let me show you how you can achieve this.
If you take a look at the
<title></title> it’s currently empty. We need to set something here. We simply cannot go to the layout file and hardcode some text in there. Otherwise, this will display on ALL pages. It needs to be dynamic.
To get the title working, we need to add the following snippet of code;
We then need to create a section called title in order for this to pull the title that we will set in the view template.
@section(‘title’) Home @endsection
It’s as easy as that. But what happens if we don’t define a section called title? If we don’t have a section, it will default back to the title, so we need to put something in place for that scenario. Take a look at the following snippet of code where we will be setting a fallback.
<title>@yield(‘title’, ‘Default Title’)</title>
We set a second value after the title using a comma. So if the section title is not defined in the view template, it will render “Default Title”. Think of it as an if-else statement.
We can simplify the title when we define the section. Have a look at this snippet of code;
That looks loads better, easier to read and we can get the code onto one line. Always look at ways to shorten the code and markup if possible. Either way is fine, I just prefer it this way.
That’s it, you now should understand how blade layout files work and how we can set a dynamic page title for our view. In the next article, we will be looking at “Sending Data to Your Views”.
I hope you’ve enjoyed this article, give it a share. If you have any comments, feel free to use the comments section or get in touch with me on my Twitter.