Part of the Twig ecosystem · 43 projects
Twig Toolkit for Drupal provides convenient Twig helpers for creating HTML classes, attributes, and inline styles within Drupal templates. It also includes an `html_element` tag that works seamlessly with Drupal's Attribute objects, making it useful for component-driven theming and design systems.
Drupal integration for studiometa/twig-toolkit: Twig helpers for HTML classes, attributes, and inline styles, plus a Drupal-aware html_element tag that works with core Attribute objects.
Machine name: twig_toolkit. No configuration UI - enabling the module registers the Twig extension.
Features
- Twig extension - Studio Meta functions and filters in Drupal Twig.
html_elementtag - accepts coreAttribute(viatoArray()), arrays, or hashes.- Thin layer - wraps the MIT upstream library; does not reinvent the helpers.
Functions: html_classes(), html_styles(), html_attributes(), merge_html_attributes(), twig_toolkit_url().
Filter: twig_toolkit_without.
Post-Installation
composer require drupal/twig_toolkitdrush en twig_toolkit -ythendrush cr- Use the helpers in theme Twig templates.
Example - classes and attributes
{% set classes = html_classes([
'button',
{ 'button--primary': is_primary },
]) %}
{% set attrs = {
class: classes,
data_component: 'Button',
} %}
<button {{ html_attributes(attrs) }}>
{{ 'Click me'|t }}
</button>
Example - html_element with Drupal Attribute
{% set attributes = attributes.addClass(
'block',
'block--featured'
) %}
{% html_element 'section' with attributes %}
{{ content }}
{% end_html_element %}
Optional with ... argument: Drupal Attribute, array, or hash.
Additional Requirements
- Drupal 10, 11, or 12
- PHP 8.3+
- Twig 3.21+ (current Drupal core)
- studiometa/twig-toolkit ^2.1
Similar projects
Integration layer around studiometa/twig-toolkit. Prefer core Attribute alone when you only need addClass() / setAttribute().
Supporting this Module
Issue queue for bugs and feature requests.
Community Documentation
Full API and more examples: README on git.drupalcode.org.
Ask your assistant about Twig Toolkit
Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works
Depends on
Dependencies of the latest stable release
No dependencies recorded for this project.
Required by
Tracked projects that depend on this one
No tracked projects depend on this one yet.
More in the Twig ecosystem
Most installed first