2026 Community Survey results are here! See how the Craft CMS community works. results are live!

Reviewing the Matrix Handler Template

The last of the three handler templates is one for all Matrix fields in the site.

The matrix tem­plate is spe­cial han­dler. Unlike the oth­er two we already dis­cussed, it doesn’t han­dle gen­er­al lay­outs. Instead, it is the gener­ic tem­plate for all Matrix fields on the site.

The code in the Matrix tem­plate is a sim­ple for-loop that loops over each block in the Matrix field and includes a tem­plate to for­mat and ren­der the con­tent that that Matrix field returns.

{% for block in matrix %}
	{% include ["matrix/" ~ block.type, "matrix/default"] %}
{% endfor %}

Like we did in ear­li­er tem­plates, we’re lean­ing on Twig’s include state­ment and feed­ing it a list of tem­plate possibilities.

Topic
Instructor
Ryan Irelan
Level
Intermediate
Date Published
February 12, 2021
Ryan Irelan

I am the creator of CraftQuest, a web developer, and former software team manager. I spend most of my time improving CraftQuest with code and courses. When I'm not in front of the computer, I spend my time with my family, and running on the roads and trails of Austin, TX.