Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:13Length:1.5 hours
Grav 1
  • Overview
  • Transcript

4.2 Post Template

In the last lesson we set up a blog listing page, and now we’ll also need to make sure our individual posts show up with the required blog-specific info rather than just like a default page. We’ll do that by creating a blog post template in this lesson.

4.2 Post Template

Hey, welcome back to Up and Running with Grav CMS. In the last lesson, we setup your blog content, and we setup the template for the blog listing. But we still need a template to control our individual blog post items. And we're gonna be able to do that very quickly, so this is gonna be a pretty short lesson. And the reason we can do it so quickly is that our item template can almost exactly match our blog template. We only need to make a couple of little changes. So what we are gonna do is make a duplicate of our blog template and we're gonna name it item.html.twig. Now we'll open that up for editing. And we'll just make a couple of little adjustments that will make this work perfectly as an item template. And we're no longer working with a collection of pages. We're just working with the one, so we don't need to set a collection variable anymore. So we'll get rid of that. We also don't need a for loop because we're no longer iterating through multiple posts. We don't need a Read More link because you're already reading more. And then the last thing to change is just replace the page summary with the full content which we do by changing this to page.content. So now let's go back to the front end of our blog and we'll click one of these Read More links. And now we have our completed item template, so it's almost exactly the same. We've just removed the extra bits of code that we didn't need and made it focus on just showing that one page item. Now one thing you might like to consider, if you're gonna be blogging regularly on a Grav CMS site, is that when you create a new page, the default template is gonna be the one that's automatically preselected. So that means that for every one of your blog posts, you'll need to manually select the item template. Now because there are no fixed rules on how you setup your template naming in Grav, if you want you might prefer to use the default template to present your blog posts. And then, for your standard pages creating a different template for that instead. So having standard.html.twig, for example, because it might get a bit annoying if every time that you create a blog post, you have to manually select that template. Just bear in mind though that if you're creating a thing for other people to use, it'll be more likely that the markdown files in their site will follow the naming protocols that we've used here. So that's just something to bear in mind for use on your own websites. It's also worth bearing in mind that if you have all of your blog posts named default.md, and then you wanna use a theme that somebody else has created, you would have to do a little bit of renaming of the template files in that theme, so it'll work with your existing content. But that's the beauty of flat file system like Grav CMS. They do have these options, so set things up to suit yourself. And if things aren't quite the way that you need them to be, it's highly accessible to make changes to the system compared to working with other platforms. So that covers the creation of our basic blog. As I mentioned in the last lesson, there's a whole bunch of extra stuff that you can add in if you want. But that gives you a foundation that covers the essentials of working with a blog in Grav CMS. So the last thing that we're gonna do with our Up and Running site is convert the homepage that we have right now, which is using just a standard page type into one that uses a modular page type. So we're gonna have three modules of distinct content. Each one of those is gonna be driven by it's own markdown file. And each one is gonna have it's own template file inside their theme. We'll be doing that across the next two lessons. And the first of those lessons is gonna get all of the critical functionality in place. And then, in the final lesson, we're going to flush out what we've done by adding content and markup into the templates that we set up. But first things first, we're gonna get the essentials of a modular page type in place in the next lesson. I'll see you there.

Back to the top