Card Teasers
Exercise: Creating twig templates for blog posts
In your Drupal site, navigate to the Homepage node
Right-click on any of the Blog posts articles within the From our blog section, and select Inspect or Inspect Element
Scroll up in the inspector's code until you find the
<article>element that wraps the entire article you clicked on. There may be multiple<article>tags within each article, but ensure you are looking at the main article wrapper. See screenshot below (click on it to zoom in):

I've marked a couple of important items in the screenshot above to ensure you are looking at the correct section in the code.
THEME HOOK: Tells you what entity you are currently looking at. In this example we are looking at the node, which is what we want since we are trying to configure the Blog nodes with the right component.
Next I have outlined all the possible template FILE NAME SUGGESTIONS Drupal is telling us we can use for this particular type of node. We know we are looking at a node as all the template suggestion names start with the word node--*. We can get as specific or general as we need to.
Finally, I have pointed out where the current template being used (
node.html.twig) is located.
Creating a template suggestion for Articles in teaser view mode
The focus at this point is to create template suggestions for all article nodes that will be displayed in the Teaser view mode. So if we look at the list of file name suggestions above we can ignore the top 4 names as those are either related to the full section of content or are extremely specific to only the single article we are looking at.
So based on the remaining names after ignoring the first 4, we can select the following name:
node--article--teaser.html.twig.node is the Drupal entity we are trying to integrate with
blog is the content type (also an entity)This names is exactly what we need to style all blog nodes that will be displayed in teaser view mode.
Now that we've selected the template we need, let's create it by making a copy from
core/themes/stable/templates/content/node.html.twiginto your theme's templates directory (/themes/custom/training_theme/src/templates/content/Rename the newly copied template as
node--article--teaser.html.twigClick your Drupal's cache
If you reload the homepage, you shouldn't really notice much difference. However, if you right-click on the same article as you did before and select Inspect or Inspect Element, and scroll to the <article> element, you should see your new template being used by Drupal to render some of the blog nodes. See below for an example:

Integrating the Card component
OK, now that our custom twig template is ready, it's time to plug it to our Card component so our blog posts start looking nice.
We'll break the integration process down so we can explain each part of it. You will find the full template at the bottom of this page.
Open node--article--teaser.html.twig in your editor and remove all the code except for the comments at the top of the template
At the bottom of the template, add the following code:
First thing we are setting a twig variable to trigger a full render of the content variable
Now let's create a twig variable for the card's title field
Why are we doing this? Well, Drupal gives us the article title text and url, but we still need to add a modifier class and a heading level. We are setting a variable so we can construct the title the same way we did when we built the heading component.
Now, let's add an
embedtwig statement for the Card component:Why use
embedand notinclude? Twig gives us 3 ways to nest or "include" templates/components into other twig templates;include,extends, andembed. Each has their pros/cons. You can learn more about them. We need to useembedinstead ofincludeto be able to use the twig blocks we added in the Card component for thedateandtagsfieldsNow let's start mapping the card's variables with Drupal fields and variables.
The first 3 items inside the
embedstatement above are Drupal specific variables so we leverage Drupal's use ofattributes,title_prefixandtitle suffix. Adding these items will allows us to make use of Drupal's contextual links to quickly edit content as well as allowing Drupal to add its attributes to our component's markup.Let's now make use of the
article_titlevariable we created aboveNext we are mapping the
image,date,body_text, andtagsvariables with Drupal fields for those elements but first we check that the fields are not empty.For the date format we are using a custom one so we can print the date in the right format.
Time to add the twig blocks for date and tag fields. Update your template as follows:
Mapping each of the components with Drupal's equivalent fields is only half the job. At least for the date and tags fields in this case. By using Twig blocks we can explicitly declare the date we want to render in the cards.
If you remember, we created two twig blocks for the date field. One for the regular card and one for the card wide. Twig blocks in the embed statement above let us pick which one the two we want to use while leaving the other one empty.
We start by declaring the
featured_datetwig block but we leave it empty. This means Drupal will not output any content for that block. Since the featured_date is only used in the card wide, we don't need to render it in the regular card.Next we declare the
card_datetwig block to print the date information.Finally we declare the
tagstwig block to render the tags.
Full integration code
Rendering blog nodes as cards in Drupal
Now that the card integration is complete, let's take a look at how the blog nodes look in Drupal.
After saving all your changes to node--article--teaser.html.twig, clear Drupal's cache
Reload the homepage
What's up with the Tags?
The card looks great but it looks like the tags are not styled at all. We'll fix them next.
Last updated