Part of the Twig ecosystem · 43 projects
Overview
Twig Style Helper extends Drupal's Attribute objects with fluent, data-URI-safe inline style manipulation methods—giving themers and developers clean .addStyle(), .removeStyle(), and .hasStyle() helpers that work just like .addClass() and .removeClass().
In core Drupal Twig templates, modifying inline styles typically requires overwriting the entire style attribute with attributes.setAttribute('style', ...) or manually concatenating strings. Twig Style Helper solves this by parsing existing inline CSS declarations into structured property-value pairs so individual CSS properties or custom properties (CSS variables) can be safely merged, overwritten, checked, or removed without clobbering existing styles.
Features
- Fluent dot-chaining on
Attributeobjects: Chain.addStyle(),.removeStyle(), and.hasStyle()directly alongside native methods like.addClass()and.removeClass(). - Automatic upgrade of template attributes: Upgrades Drupal's default
attributes,title_attributes, andcontent_attributesvariables as well as objects created withcreate_attribute(). - Twig filter & function support: Also exposes
|addStyle,|removeStyle,addStyle(), andremoveStyle()for anyDrupal\Core\Template\Attributeinstance. - Data-URI & semicolon safe: Uses a quote- and parenthesis-aware parser so semicolons inside
url("data:image/...")or CSScontentstrings never break declaration splitting. - Automatic inline SVG encoding: Detects unencoded
<svg>markup insidedata:image/svg+xmlURIs and URL-encodes special characters automatically so Drupal's HTML attribute escaping never corrupts inline SVG backgrounds.
Usage Examples
1. Dot-chaining with create_attribute()
{% set wrapper = create_attribute() %} <div{{ wrapper.addStyle({ '--accent': brand_color, 'background-image': 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://w3.org\' viewBox=\'0 0 10 10\'><circle cx=\'5\' cy=\'5\' r=\'5\'/></svg>")' }).addClass('wrapper') }}>
2. Modifying Drupal's built-in attributes
{# Accepts associative arrays or raw CSS strings #} <article{{ attributes.addStyle('padding: 1rem;').removeStyle('margin').addClass('node') }}>
3. Checking for existing styles with hasStyle()
{% if attributes.hasStyle('background-image') %} <div{{ attributes.addClass('has-background') }}> {% endif %}
4. Using Twig filters or functions
{# Filter syntax #} <div{{ attributes|addStyle({'color': 'red', 'padding': '1rem'})|removeStyle('margin').addClass('box') }}> {# Function syntax #} <div{{ addStyle(attributes, 'color: red;').addClass('box') }}>
Requirements
- Drupal 10 or Drupal 11
- No external dependencies
Installation
Install as you would normally install a contributed Drupal module:
composer require drupal/twig_style_helper drush en twig_style_helper drush cr
AI Disclosure
Generative AI tools (Google Gemini) were used during the development of this module to assist with boilerplate scaffolding, code documentation, and project artwork. All code, architecture, and functionality have been reviewed, tested, and verified by the project maintainer.
Other great Twig enhancing projects:
Ask your assistant about Twig Style Helper
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
Activity
Releases
| Version | Type | Core | PHP | Release date | |
|---|---|---|---|---|---|
| 1.0.x-dev | Dev | 10–11 | Nov 26, 2026 |