Flexible Twig Templates in Craft 3

Reviewing the Item Handler Template

The item handler templates let us display individual entries in a listing view.

The List­ing Item Tem­plate, which we’ve named item.twig in the imple­men­ta­tion of the sam­ple site Down­link, han­dles the con­tent lay­outs for the list­ing pages, like cat­e­go­ry pages or entries by tag. This includes the home­page, where we have a list­ing of the dif­fer­ent con­tent from the site (two types of Blog con­tent and Pod­cast episodes).

The rea­son we have the List­ing Item Tem­plate sep­a­rate from the Entry Tem­plate is so we can dis­play our entry con­tent in a dif­fer­ent way on list­ing pages. Some­times, for exam­ple, list­ing pages, like a home­page or cat­e­go­ry page, have trun­cat­ed entry con­tent or excerpts with a link to read more. If we used the full Entry tem­plate to han­dle this then we’d have to make that tem­plate han­dle more than one sce­nario, which increas­es the com­plex­i­ty of our templates.

Like the Entry tem­plate we just dis­cussed, the List­ing Item Tem­plate uses an include state­ment and a list of dynam­i­cal­ly-gen­er­at­ed tem­plate names. 

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

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