Lessons: 14Length: 1.6 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Using Front Matter Defaults

So far, you have seen how to use Front Matter for individual posts and pages. But there is a problem. What happens if you want to change a configuration setting for all your blog posts, but you happen to have hundreds of them? In this lesson, I'll show you how to centralize some common configuration settings in the _config.yml file.

3.3 Using Front Matter Defaults

All right, the site's looking pretty good so far, but now I wanna tweak the way that we handle these blog posts just a little bit. So we did add in the read time and the sharing links for just this particular post. And the problem with that is, now the users are expecting that, but they're only getting it for that one post, we wanna add it to everything. But I don't wanna go through every single post that I have and add that information in there, that would just take way too much time, that's a real pain, I don't wanna do it. So luckily for us, when it comes to Jekyll and using GitHub Pages we don't have to do that. What we can do is, we can take of something called front matter defaults. Now, lets head back to our documentation over here. And if we go back into the Content, and go into Working with Posts, you'll see the information that we started working with before. We have the posts folder, then the naming structure, but it also recommends using front matter defaults. So let's just go ahead, and copy this section here right now, and let's bring it into our code, and then let's talk about it a little bit. So I'm gonna come back into my config file, and I'm going to drop down in here, and I'm going to paste it in. So now I've added in a defaults section. Now, what this says is that I'm gonna create some default configurations that can be used as front matter in different types of pages that I have on my site. So let's walk through this a little bit. So this is just for documentation purposes, and this is actually something highly recommended, cuz you can create several different default configurations for different scopes. In this case I'm doing it for my posts, and I'm doing this so that I know it, but I could create another one that's just for pages. I could create a whole customized section called tutorials, and I could create a customized default section for tutorials, so this is just a nice way to keep things nice and neat. So what you do in the defaults section is you create scopes, and you can create any number of these scopes that you want. Now, under scope the first two important things that you define is a path and type. Now, path is meant to define where Jekyll should look to find the content you want to apply this configuration to. So usually what you'll see here is an empty string, just double quotes. What that means is that, look at all the files in my project, look at everything. So if you're good about keeping everything in a specific location, you could specify a specific subfolder, a specific path, that's fine. But sometimes I tend to pepper things all over the place, and maybe I wanna pick all those things up, then I would use this empty string, these double quotation marks. Then, I need to specify the type. So in this case, I want to specify this particular configuration for all posts. If I was making it for pages, it would be for pages, things like that. So once you're done that, once you've specified your scope, now you can specify the values that you want them to be. So let's go back and take a look here very quickly. So things that you would leave in the front matter of the individual pages are gonna be things like title, permalink, published. Those are the things that you're probably gonna wanna keep specific to each one of those posts, because they're gonna change from post to post to post. Now, things like layout, or read_time, or share are things that you would probably want to be consistent across all of your posts. And there's other configuration settings you can add in there, but those are just some very common ones. So if I come back to my config, you're gonna see some samples here. So the layout, I've already got that in here, I'm specifying that as being single, read_time, true, comments. comments I'm gonna leave that off, so I don't really want comments right now, but you can add in a comments section to your post. share, we wanna have on. related, that's kind of interesting. What that really is gonna do is, it's going to make an attempt to show related posts on your blog so that somebody can go and read those related posts. Unfortunately it doesn't work really well, it really just shows the recent posts, so I typically leave that off as well. author_profile is an interesting one, we're gonna leave that on there. So what this is gonna say is, I'm gonna show the author profile for all the posts. So over on the left-hand side of the page you're gonna see an image of the author, if you've configured that, and we'll talk about that in the next lesson, and some other information about the author. But right now we're just gonna set that to true, cuz we're gonna wanna fill that in in a little bit. So this is all we need. So you have the layout, author_profile, read_time, and share. So let's go ahead and save that. Now I can come back to this specific post and I can get rid of these, I no longer need to put them in here. Now, one thing that you may want to pay attention to when you're doing this, is that the best practice is to provide all of those front matter defaults in that configuration file. And then, anytime you want to override any of those defaults you can come into the specific post and change that value. So maybe for some reason I didn't wanna show the author profile for this particular post, or maybe I wanted to change the layout, then I could specify the override in here, and then it would override the defaults. So the defaults, like I said before, are really just there to be kind of a broad brush stroke on everything that meets this scope criteria, and then apply these values to all the front matter of all of those particular items. So once I have all of these things saved I'm actually gonna go in here, and I'm going to commit these. So I'm gonna say, Use Defaults for posts. I'm gonna save that, I'm gonna commit that, and then I'm gonna push it up to GitHub. And then, once that has all been completed and my site has been rebuilt, then I should be able to see all of these changes manifest themselves on my site. So hopefully, after just a few moments of putting in your changes and refreshing your page, you should now see that we have our read times on all of our posts under Recent Posts on our blog page. So if were I click on one of these now, I should see that I have the read time. And I can come down here, and I have the share buttons. And if I go to my previous posts, and once again that's one of those things that you get for free just by having this particular template, I see that I have read times here now as well. And if I go down to the bottom, I also have the share buttons. So we're really starting to come along, we're really starting to optimize the changes that we have to make, and that's really good, but it still seems like we're missing something, and what is that? Well, I did mention that we have added in this author_profile true to all of our pages, but we seem to be missing that. Where we should be seeing that is over here on the left-hand side, but there's nothing showing up here. Why is that? Well, that's because we haven't specified any information about the author of this post. But that's what we're gonna start to do in the next lesson, we're gonna add in some author details and talk about some other configuration options.

Back to the top