This project is not covered by Drupal’s security advisory policy.
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') }}>
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:
Project information
- Project categories: Content display
- Created by skaught on , updated
This project is not covered by the security advisory policy.
Use at your own risk! It may have publicly disclosed vulnerabilities.
