PHPackages                             parisek/twig-attribute - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. parisek/twig-attribute

ActiveLibrary[Templating &amp; Views](/categories/templating)

parisek/twig-attribute
======================

A Twig extension for adding attributes to an element

v1.5.0(1y ago)32.3k↓50%11GPL-2.0-or-laterPHP

Since Jan 17Pushed 6mo ago1 watchersCompare

[ Source](https://github.com/parisek/twig-attribute)[ Packagist](https://packagist.org/packages/parisek/twig-attribute)[ Docs](https://github.com/parisek/twig-attribute)[ RSS](/packages/parisek-twig-attribute/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (9)Used By (1)

Twig Attribute Extension
========================

[](#twig-attribute-extension)

Twig is desperately missing wrapper function to handle HTML Attributes. I borrowed great [Attribute](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Template%21Attribute.php/class/Attribute/11.x) class from Drupal. It collects, sanitizes, and renders HTML attributes in a nice way.

This package was created based on issue [\#2664570 - Move Attribute classes under Drupal\\Component](https://www.drupal.org/project/drupal/issues/2664570) which created groundwork for using this class outside Drupal world. Unfortunately I had to copy code out because issue is still open. I hope issue will be merged soon, so I can switch to official component split off from Drupal core with proper attribution. Expected component should be available at

Installation
------------

[](#installation)

Twig Attribute Extension can be easily installed using [composer](http://getcomposer.org/)

```
composer require parisek/twig-attribute

```

Usage
-----

[](#usage)

```
$twig = new Twig_Environment($loader);
$twig->addExtension(new Parisek\Twig\AttributeExtension());
```

To use in a symfony project [register the extensions as a service](http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service).

```
services:
  twig.extension.attribute:
    class: Parisek\Twig\AttributeExtension
    tags:
      - { name: twig.extension }
```

Template
--------

[](#template)

```
{% set my_attribute = create_attribute() %}
{%
  set my_classes = [
    'kittens',
    'llamas',
    isKitten ? 'cats' : 'dogs',
  ]
%}

  {{ content }}

```

```

  {{ content }}

```

Examples were copied from [official Drupal documentation](https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates).

Use Cases
---------

[](#use-cases)

- [Drupal - Pattern Lab](https://patternlab.io/)
- [Wordpress - Timber](https://wordpress.org/plugins/timber-library/)
- [Pimcore - Templates](https://pimcore.com/en)

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance52

Moderate activity, may be stable

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~226 days

Recently: every ~254 days

Total

7

Last Release

585d ago

PHP version history (2 changes)v1.0.0PHP &gt;=7.3.0

v1.4.0PHP &gt;=8.1.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0f60a7484412a046d60bd8de9c5fb773cd9d59f00a1b455284fa718b786a4d93?d=identicon)[parisek](/maintainers/parisek)

---

Top Contributors

[![parisek](https://avatars.githubusercontent.com/u/10908003?v=4)](https://github.com/parisek "parisek (10 commits)")

---

Tags

drupaldrupal-10drupal-9phppimcore-bundlesymfony-bundletimbertwigtwig-attributetwig-extensiontwigattribute

### Embed Badge

![Health badge](/badges/parisek-twig-attribute/health.svg)

```
[![Health](https://phpackages.com/badges/parisek-twig-attribute/health.svg)](https://phpackages.com/packages/parisek-twig-attribute)
```

###  Alternatives

[twig/extra-bundle

A Symfony bundle for extra Twig extensions

91492.0M315](/packages/twig-extra-bundle)[twig/intl-extra

A Twig extension for Intl

36763.2M221](/packages/twig-intl-extra)[rcrowe/twigbridge

Adds the power of Twig to Laravel

9105.9M50](/packages/rcrowe-twigbridge)[twig/string-extra

A Twig extension for Symfony String

22046.0M133](/packages/twig-string-extra)[twig/cssinliner-extra

A Twig extension to allow inlining CSS

22918.5M55](/packages/twig-cssinliner-extra)[symfony/ux-twig-component

Twig components for Symfony

21914.8M162](/packages/symfony-ux-twig-component)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
