Flexible Twig Templates in Craft 3

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.

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