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 Attribute objects: 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, and content_attributes variables as well as objects created with create_attribute().
  • Twig filter & function support: Also exposes |addStyle, |removeStyle, addStyle(), and removeStyle() for any Drupal\Core\Template\Attribute instance.
  • Data-URI & semicolon safe: Uses a quote- and parenthesis-aware parser so semicolons inside url("data:image/...") or CSS content strings never break declaration splitting.
  • Automatic inline SVG encoding: Detects unencoded <svg> markup inside data:image/svg+xml URIs 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,&lt;svg xmlns=\'http://w3.org\' viewBox=\'0 0 10 10\'&gt;&lt;circle cx=\'5\' cy=\'5\' r=\'5\'/&gt;&lt;/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

Releases