Reviewing the Entry Handler Template

Diving in, we look at how the Entry Handler Template works.

The first thing we see in the Entry tem­plate is that it is includ­ing anoth­er template. 

{% include ["entry/" ~ entry.section.handle ~ "/" ~ entry.type, 
"entry/" ~ entry.section.handle ~ "/default", "entry/default"] %}

Which one? Well that depends on the sec­tion and entry type. The include uses a set of tem­plates — defined via vari­ables — to hand the tem­plate lay­out off to the prop­er template.

Let’s say we are going to dis­play a Link entry from the Blog sec­tion. The entry.twig tem­plate will include the tem­plate entry/blog/link.twig.

This link.twig tem­plate extends anoth­er tem­plate (entry/default.twig), which has some of the markup we need. This entry/default.twig is gener­ic is used by every type of field on the site.

After that the tem­plate defines a block called itemContent and over­rides it with the out­put of the linkDescription field for that entry.

Over in the entry/default.twig tem­plate there is a con­di­tion­al that dis­plays the entry title dif­fer­ent if the Entry Type is link.

{% if entry.type == 'link' %}
	<h2><a href="{{ entry.linkUrl }}">{{ entry.title }} &neArr;</a></h2>
{% else %}
	<h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>
	<p class="post-date">{{ entry.postDate | date('M d, Y') }}</p>
{% endif %}

The entry/default.twig tem­plate also extends the _layout.twig tem­plate, which is the base lay­out for the entire site.

We now have a sin­gle tem­plate to which we point our Sec­tions and the we lever­age Twig to take it from there. This sim­pli­fies our approach so we don’t have to have mul­ti­ple entry tem­plates — one for each sec­tion — and instead leave the speci­fici­ty for lat­er on when we get low­er in the tem­plate stack.

Here are all of the videos in Flexible Twig Templates in Craft 3:

1Introduction to the CourseRyan introduces the course, covering what we need and what we'll learn. 03:49
2What We're BuildingWe review the Downlink website, the project we'll build for this course. 02:02
3Reviewing the Template StackLet's use construction paper and labels to review how the template stack works. 05:09
4Stepping Through the Template Stack CodeLet's get familiar with the code that will make up our template stack. 05:03
5Introducing the Handler TemplatesThe handler templates are what kick off the requests to the other supporting templates in the stack. 02:24
6Reviewing the Entry Handler TemplateDiving in, we look at how the Entry Handler Template works. 05:35
7Reviewing the Item Handler TemplateThe item handler templates let us display individual entries in a listing view. 06:52
8Reviewing the Matrix Handler TemplateThe last of the three handler templates is one for all Matrix fields in the site. 04:08
9Preparing to Code the Template StackThe project setup you'll need to do to follow along and code with me. 01:29
10Reviewing the Layout TemplateThe layout template has all of the global markup and code for the site. 03:10
11Creating the Homepage Listing TemplateWe start building the template components with the homepage. This covers building out the 06:38
12Creating the Included TemplatesThe handler templates hand off to templates via includes. Let's build those included templates for our listing pages. 07:57
13Finishing the Homepage Listing TemplateWe wrap up the homepage listing template by creating additional included templates. We start to see the magic happen. 07:47
14Completing the Matrix Handler TemplateWe started the Matrix handler template earlier in the course so we could continue our work. Let's finish it up so it works as we need. 07:21
15Creating the Entry Handler TemplateSo far into the course and we're just doing Entry templates? Yes, but all of this prep work is going to speed things up. Let's make the entries come alive! 10:42
16Testing the Template StackAll of our work pays off as we add a new section to the site and see the flexible template stack react and work its magic! 05:56

Loading next video...