Introducing the Handler Templates

The handler templates are what kick off the requests to the other supporting templates in the stack.

Han­dler tem­plates are site-wide tem­plates that han­dle all tem­plate behav­ior for a lay­out. Han­dler tem­plates don’t typ­i­cal­ly do much more than han­dle the ini­tial tem­plate request and then pass the work off to anoth­er lay­er of tem­plates for the actu­al con­tent rendering. 

Sim­i­lar to the _layout tem­plate we use for the wrap­per for our site, the han­dler tem­plates are gener­ic enough to be reused over and over again by ren­dered pages that have dif­fer­ent content. 

The han­dler tem­plates are most­ly con­tent agnos­tic (just like the site’s _layout tem­plate). They are set up in a way that allow them to han­dle dif­fer­ent types of con­tent from dif­fer­ent Sec­tions in Craft, regard­less of Entry Type or the fields used.

The tem­plates in our Down­link sam­ple site that are han­dler tem­plate are:

  • entry.twig — This tem­plate han­dles all of the requests for indi­vid­ual entry views of all sec­tions of the site (except the Home­page Sin­gle section).
  • item.twig — This tem­plate han­dles all dis­play of con­tent on a list­ing page, regard­less of which sec­tion is being dis­played. We use this tem­plate on the home­page to dis­play the lat­est entries.
  • matrix.twig This tem­plate han­dles all dis­play of Matrix fields across the site. In our sam­ple site we only have one Matrix field for the body field in the Default Entry Type. How­ev­er, if and when we have anoth­er Matrix field this han­dler tem­plate will be the first touch point of the lay­out process. Like the oth­er two han­dler tem­plates, matrix.twig only han­dles the request to dis­play a Matrix field, and then quick­ly hands that off to the appro­pri­ate field.

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...