We walk through how to create an entry template in Twig that support a content builder field using Matrix and switch statements.
With our homepage coded, and the testimonials work complete, we’re ready to tackle the adventures single entry template. This is the template that will show an individual adventure with the content, stats, and images.
Going back to the control panel, let’s refresh our memories on what we’ve built. We have the following fields:
The content builder is made up of a rich text field (via the Redactor plugin), a CTA block with a text and URL field, and a stats block, powered by table field with a name and value.
Let’s first set up our extended layout template in the _entry.twig
template.
{# Adventures Entry Template #}
{% extends '_layouts/_wrapper.twig' %}
{% block main %}
{% endblock %}
The next step is to grab the static template you downloaded as part of this course. Look for the template called adventure-entry.html
.
Grab everything between and including the main
element and paste it in between the main block in the _entry.twig
template.
Okay, now we’re ready to make the static content dynamic!
Since we’re in a single entry view, and Craft knows that this template is the entry view template for the Adventures section per our section’s settings, we automatically access the entry
object with all of the entry data. Craft knows this based on the URI requested by the browser.
Just like with Singles, we don’t have to query for the entry data; we just have it. Very handy! That means we can access the adventure content using entry
and then a property for the field handle.
We’ll start at the top with the title.
<h1
class="mt-2 text-3xl leading-8 font-extrabold tracking-tight text-gray-900 sm:text-4xl"
>
{{ entry.title }}
</h1>
And then the adventure description:
<div class="text-base max-w-prose mx-auto lg:max-w-none">
<p class="text-lg text-gray-500">
{{ entry.description }}
</p>
</div>
Those are the easy ones. Let’s tackle the content builder now, which is powered by a Matrix field.
The first thing we want to do is identify the markup for each of our different block, and then label that markup using Twig comments. This will make it easier to code the Matrix field in Twig since we’ll already know which markup powers which block.
Then we can query for our Matrix field and iterate over the blocks. We’ll use a switch statement to do this because it’s simpler than using conditional to check for the block type. We’ll switch the block type and output the correct markup and Twig for that block.
First, let’s query for our Matrix field:
{% for block in entry.contentBuilder.all() %}
{% switch block.type %}
{% endswitch %}
{% endfor %}
Starting with the stats block…
{# Stats #}
{% case 'stats' %}
<div class="mt-10">
<dl class="grid grid-cols-2 gap-x-4 gap-y-8">
{% for stat in block.stat %}
<div class="border-t-2 border-gray-100 pt-6">
<dt class="text-base font-medium text-gray-500">{{ stat.name }}</dt>
<dd class="text-3xl font-extrabold tracking-tight text-gray-900">{{ stat.value }}</dd>
</div> {% endfor %}
</dl>
</div>{# Stats End #}
And then the rich text block:
{# Rich Text #}
{% case 'richText' %}
<div
class="mt-5 prose prose-indigo text-gray-500 mx-auto lg:max-w-none lg:row-start-1 lg:col-start-1">
{{ block.text }}
</div>
{# Rich Text End #}
And then the CTA:
{# Call to Action #}
{% case 'cta' %}
<div class="mt-10">
<a href="{{ block.linkUrl }}" class="text-base font-medium text-primary">{{ block.linkText}} <span aria-hidden="true">→</span> </a>
</div>{# Call to Action End #}
Next up, is the images along the right side of the page in the desktop view.
{# Images #}
{% set images = entry.images.all() %}
<div class="relative lg:row-start-1 lg:col-start-2">
<div class="relative text-base mx-auto max-w-prose lg:max-w-none">
{% for image in images %}
<figure class="mb-8">
<div class="aspect-w-12 aspect-h-7 lg:aspect-none">
<img class="shadow-lg object-cover object-center lg:rotate-1 border-gray-200 border-8"
src="{{ image.url }}"
alt="{{ image.title }}"
width="1184"
height="1376"
/>
</div>
</figure>
{% endfor %}
</div>
</div>
{# End Images #}
We also have the locations listing at the bottom. Let’s abstract that out into a shared template so we can use the same code we created for the homepage on any templates that need to have the locations listing.
First, we’ll create a new directory called _shared
, and inside of it a new template named _locations.twig
.
Let’s grab the locations code, from the section
element wrapping that output, from the homepage and place it on the new template.
We have to include the new shared template in the homepage index.twig
template so the locations still appear as expected.
{% include '_shared/_locations.twig' %}
And, then we’ll do the same on the adventures entry template, replacing the static markup and content with the include tag.
Real World Craft CMS is made up of the following videos: