https://github.com/parisek/twig-attribute
Twig Attribute Extension - collects, sanitizes, and renders HTML attributes in a nice way.
https://github.com/parisek/twig-attribute
drupal drupal-10 drupal-9 php pimcore-bundle symfony-bundle timber twig twig-attribute twig-extension
Last synced: 28 days ago
JSON representation
Twig Attribute Extension - collects, sanitizes, and renders HTML attributes in a nice way.
- Host: GitHub
- URL: https://github.com/parisek/twig-attribute
- Owner: parisek
- License: gpl-2.0
- Created: 2021-01-16T17:02:34.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-10T08:42:02.000Z (12 months ago)
- Last Synced: 2025-03-18T17:24:41.443Z (7 months ago)
- Topics: drupal, drupal-10, drupal-9, php, pimcore-bundle, symfony-bundle, timber, twig, twig-attribute, twig-extension
- Language: PHP
- Homepage: https://packagist.org/packages/parisek/twig-attribute
- Size: 23.4 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
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 [https://github.com/drupal/core-attribute](https://github.com/drupal/core-attribute)
## Installation
Twig Attribute Extension can be easily installed using [composer](http://getcomposer.org/)
composer require parisek/twig-attribute
## Usage
```php
$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).
```yaml
services:
twig.extension.attribute:
class: Parisek\Twig\AttributeExtension
tags:
- { name: twig.extension }
```## Template
```twig
{% set my_attribute = create_attribute() %}
{%
set my_classes = [
'kittens',
'llamas',
isKitten ? 'cats' : 'dogs',
]
%}
{{ content }}
``````twig
{{ content }}
```Examples were copied from [official Drupal documentation](https://www.drupal.org/docs/8/theming-drupal-8/using-attributes-in-templates).
## Use Cases
- [Drupal - Pattern Lab](https://patternlab.io/)
- [Wordpress - Timber](https://wordpress.org/plugins/timber-library/)
- [Pimcore - Templates](https://pimcore.com/en)