Twig Tips: Building Fast and Flexible Drupal Themes the Right Way

Twig is the powerful templating engine that makes Drupal theming flexible, clean, and more secure. Whether you're crafting a pixel-perfect marketing site or a dynamic user dashboard, mastering Twig can take your Drupal theme development from basic to brilliant.
Here’s how to get it right.
Understand How Twig Works in Drupal
Twig replaces PHPTemplate in Drupal 8 and onwards. It separates logic from markup, offering clean templates and improved security. Every Drupal theme uses .html.twig
files to render page elements—from blocks and nodes to regions and menus.
Keep Templates Simple and Focused
Avoid placing too much logic in your Twig files. Let preprocess functions handle the heavy lifting. This keeps your templates clean and easier to maintain. For example, calculate values or manipulate arrays in YOURTHEME.theme
and pass only what's needed to Twig.
Use Twig Filters and Functions Wisely
Twig comes with many built-in filters and functions like |escape
, |length
, |join
, and include
. Learn and use them to manipulate content cleanly. They allow formatting, conditional logic, and modular template structures.
Enable Debugging for Theme Development
Enable Twig debug mode in your services.yml
file. This lets you inspect theme suggestions and template file names right in the HTML source, saving tons of guesswork when customizing layouts.
Extend Templates with block
and extends
Twig supports inheritance using extends
. Use base templates and override only what's necessary in child templates. This keeps your theme modular and DRY (Don’t Repeat Yourself).
Avoid Overusing raw
or escape(false)
These can expose your site to XSS vulnerabilities. Always sanitize data before outputting unless you absolutely trust the source—and even then, double-check.
Bonus Tip: Use Libraries and Classes Smartly
Pair your Twig files with proper CSS/JS libraries using *.libraries.yml
. Add conditional classes in preprocess functions and print them with {{ attributes.addClass(...) }}
for better theming flexibility.
Need help building or refining your Drupal theme?
You can hire expert Drupal developers to design or optimize your theme for performance, accessibility, and brand consistency. Or book a free consultation with our UI/UX engineers to review your current setup.
Frequently Asked Questions
What is Twig in Drupal used for?
Twig is the default templating engine in Drupal, used to render HTML output in a secure and structured way.
Can I use PHP in Twig templates?
No, Twig does not allow raw PHP in templates, which improves security and separates logic from presentation.
How do I find which Twig template is being used?
Enable Twig debugging in services.yml
to view template suggestions directly in your HTML source.
Why is Twig better than PHPTemplate?
Twig offers cleaner syntax, better security, template inheritance, and easier maintainability compared to the older PHPTemplate system.
What’s the best way to pass variables to Twig templates?
Use preprocess functions in your .theme
file to prepare and pass custom variables to your Twig templates.