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.

Transcript

Premium

Follow along with the full transcript

Search every word, click any line to jump to that moment, and read along as you watch — a CraftQuest Premium feature.

Sign up for Premium
Topic
Instructor
Level
Intermediate
Date Published
February 12, 2021