Flexible Twig Templates in Craft 3

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.

Flexible Twig Templates in Craft 3 is made up of the following videos: