Twig Blocks
What are Twig Blocks?
Twig blocks are used for inheritance and act as placeholders and replacements at the same time. Twig blocks can be thought of as holes to fill in in a Drupal page. Or perhaps as regions in your pages.
The challenge we face is having control of the markup while adhering to Drupal's best practices for rendering content. When the time to integrate a component with Drupal comes, oftentimes using include
statements will do the job, but there are times when we want to modify content or markup before Drupal renders a component and include
statements don't allow for this. We could use the extends
Twig statements but these could also be limiting. In these situations the best option is to use Twig's embed
statements. Embed statements combine the functionality of both, include
and extends
statements. Let's see an example.
We've declared a twig block (card_extra_content
), in which we can add or remove any content we want. The block on its own does nothing. Currently we added the Twig block as a placeholder to use later.
IMPORTANT: Twig blocks are not the same, or have anything to do with Drupal blocks.
The code above shows how we can use the Card component and we can make use of the Twig Block. This gives us a lot of flexibility and control to alter content before rendering. We will see other examples of Twig Blocks throughout this training.
Last updated