The handler templates are what kick off the requests to the other supporting templates in the stack.
Handler templates are site-wide templates that handle all template behavior for a layout. Handler templates don’t typically do much more than handle the initial template request and then pass the work off to another layer of templates for the actual content rendering.
Similar to the
_layout template we use for the wrapper for our site, the handler templates are generic enough to be reused over and over again by rendered pages that have different content.
The handler templates are mostly content agnostic (just like the site’s
_layout template). They are set up in a way that allow them to handle different types of content from different Sections in Craft, regardless of Entry Type or the fields used.
The templates in our Downlink sample site that are handler template are:
entry.twig— This template handles all of the requests for individual entry views of all sections of the site (except the Homepage Single section).
item.twig— This template handles all display of content on a listing page, regardless of which section is being displayed. We use this template on the homepage to display the latest entries.
matrix.twigThis template handles all display of Matrix fields across the site. In our sample site we only have one Matrix field for the
bodyfield in the Default Entry Type. However, if and when we have another Matrix field this handler template will be the first touch point of the layout process. Like the other two handler templates,
matrix.twigonly handles the request to display a Matrix field, and then quickly hands that off to the appropriate field.
Here are all of the videos in Flexible Twig Templates in Craft 3:
|1||Introduction to the Course||Ryan introduces the course, covering what we need and what we'll learn.||03:49|
|2||What We're Building||We review the Downlink website, the project we'll build for this course.||02:02|
|3||Reviewing the Template Stack||Let's use construction paper and labels to review how the template stack works.||05:09|
|4||Stepping Through the Template Stack Code||Let's get familiar with the code that will make up our template stack.||05:03|
|5||Introducing the Handler Templates||The handler templates are what kick off the requests to the other supporting templates in the stack.||02:24|
|6||Reviewing the Entry Handler Template||Diving in, we look at how the Entry Handler Template works.||05:35|
|7||Reviewing the Item Handler Template||The item handler templates let us display individual entries in a listing view.||06:52|
|8||Reviewing the Matrix Handler Template||The last of the three handler templates is one for all Matrix fields in the site.||04:08|
|9||Preparing to Code the Template Stack||The project setup you'll need to do to follow along and code with me.||01:29|
|10||Reviewing the Layout Template||The layout template has all of the global markup and code for the site.||03:10|
|11||Creating the Homepage Listing Template||We start building the template components with the homepage. This covers building out the||06:38|
|12||Creating the Included Templates||The handler templates hand off to templates via includes. Let's build those included templates for our listing pages.||07:57|
|13||Finishing the Homepage Listing Template||We wrap up the homepage listing template by creating additional included templates. We start to see the magic happen.||07:47|
|14||Completing the Matrix Handler Template||We 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|
|15||Creating the Entry Handler Template||So 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|
|16||Testing the Template Stack||All 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|