What is the use of extends in twig file in drupal 9

By toswebdev, 14 May, 2022
What is the use of extends in twig file in drupal 9

Twig's Extends allows you to inherit all the code from a parent template without having to duplicate it AND replaces parts of it (the parts in Twig blocks). You can only replace parts that are Twig Blocks. If you try and replace anything else in the parent template, you will get an error.

Twig templates can be extended with the following syntax -

{% extends 'example.html.twig' %}

Drupal modules and themes are automatically namespaced. To extend a template from another theme or module (e.g. here from the classy theme), use the machine name:

{% extends "@classy/block/block--system-menu-block.html.twig" %}

 

Comments

All comments go through moderation, so your comment won't display immediately.