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.
Project information
Minimally maintained
Maintainers monitor issues, but fast responses are not guaranteed.- Project categories: Integrations
22 sites report using this module
- Created by bronismateusz on , updated
Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
