Unlimited WordPress themes, plugins, graphics & courses! Unlimited asset downloads! From $16.50/m
FREELessons:13Length:1.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.9 Create an Entry Template

On top of the default templates expected to be included in a Bolt theme, there’s the option to create templates specifically for different types of content. In this lesson you’ll learn how to create a template that applies only to the “contenttype” named “entry”, as well as how to make it display content from a special video field found in “entries”.

2.9 Create an Entry Template

Hi and welcome back to Bolt CMS theme development. So far, we have, technically speaking, completed our Bolt theme. We've got all the templates in that need to be in for all of the default functionality. But there are some cool extras that you can do with the Bolt theme. So we're gonna move on to doing a couple of those now. And what we're gonna do in this video is create a template that will be picked up based on the slug of the content type that's being viewed in the front end. Now each of the templates that we have created so far are the ones that were used by Bolt by default. However, let's say there's something custom that you want to do for a specific content type. So this is a standard record presentation for an entry. So we have our body and we've got our image, and we've got our title. And if we look in the back end, this is the same entry that we were just looking at. So there's our title and there's our body. And if you go into the Media tab, there's our image. However, if you scroll down a little further, you can see that there's also a video field. Now right now with that default record template, even if you put a video in here, it wouldn't show up. So we're gonna create a template for entries only that will pick up on this video field. And we'll display a video if one's available. So to start with we're just gonna add in a URL of a YouTube video so make sure you pick out one of the entries that you have in your offline Bolt side. And just add in the URL for any YouTube video that you like. And then we'll save that. All right. And it has picked up the video successfully. You'll see a preview image over here if that's all working. All right, so into our theme files. Now, the code we're going to be using would be mostly the same as what's already in our record template. So duplicate that. And now we need to rename it. And the name that we have to give it has to exactly match the slug of the content type when you are viewing a single record in that content type. So we can look up here, and we can see that the slug is entry. So we'll copy that, And we'll name this template file entry.twig. And what we're gonna do is gonna be pretty straightforward. We're not gonna do a lot of steps, we're just gonna do something basic. First we're gonna check if the video field has been filled in with something. So we're going to check that it's not empty. And then if it's not empty, we'll show the video. If it is empty, we'll show the image instead. So we just make a space after the heading and we'll add if record.video is not empty, and then we'll add else and then we'll grab our image code and indent it. And then after that we'll close off with endif. And then to output our video, we'll make some space up here, and you'll be able to guess most of what we're going to use here to output the video. We'll use our standard curly brackets, and then we're going to use record.video. But in this case we gonna add one extra thing. We want this video to be responsive. So we're also going to add in .responsive. All right, so now we go back to the article that we just edited a YouTube video. It should show up there, and on all of our other entries we should see an image. And there's our video. And then if we head back to our home page and we look at a different entry, we've still got our image there. So just quickly also, if when you're testing this, you find that your video doesn't appear right away, I found that sometimes it can actually take hitting this Save button a couple of times before the video then shows up. So if you don't see it, don't panic. Just hit that a couple of times and check out the preview. Once you see the video come up in the preview, then go back to your proper front end again and refresh once more, and then you should see the video. So you can use that technique of creating a template with a name that matches the slug for any content type. So if you wanted a custom template to display pages, you would go into a single page record and check the slug. And you'd see there that it's page. So you'd make a template named page.twig. And the same thing would apply for a showcase record or any other content type that might be in any Bolt site that you're working on. So that wraps up all of the templates, all the templates are done that we're gonna be doing for this course. But we still got a couple of extras to add in to properly round out our theme. And one of those things is handling what to do when an article that a person looks for is not found. So for example, right now, if we just put some random bleh on the end of this URL and try to view it, this is what we get. Obviously this is not ideal. So in the next lesson, I'm gonna show you how to set up your thing to handle URLs that are not found. I'll see you there.

Back to the top