12

Creating Custom Twig Functions and Filters

Now that the demo website is fully styled, we can move on to some more advanced topics. The first one is the ability to extend Twig with custom functions and filters.

Twig is baked with a highly extensible framework that allows us to do customizations to simplify our templates by delegating the complex logic away from the theme layer.

In this chapter, we’re going to cover the following main topics:

  • Creating a custom Twig function
  • Creating a custom Twig filter

Technical requirements

To run the examples in this chapter, make sure to follow the instructions provided in Chapter 1, Setting Up a Local Environment.

You can find the code for this chapter in the GitHub repository: https://github.com/PacktPublishing/Modernizing-Drupal-10-Theme-Development ...

Get Modernizing Drupal 10 Theme Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.