piedweb / render-html-attributes
[Plates and Twig Extension] Transform an array in html tag attributes.
Installs: 39 777
Dependents: 2
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- phpunit/phpunit: ^9
- twig/twig: ^3
- vimeo/psalm: ^4.3
README
This package is an extension for both Twig and Plate engine Plates.
Plates is not anymore supported since v1.0.3.
Two features for the same goal Manipulate html tag attributes via object/PHP array :
attr({class: "col", id: "piedweb", data-content:"Hello :)', ...})
transform an array in html tag attributesmergeAttr($attributes1, $attributes2, [$attributes3, ...])
merge multiple array without loosing values (Eg. :['class' => 'main']
+['class' => 'content']
=['class' => 'main content']
)
Table of contents
Usage
For Twig :
Load the extension in twig (eg for symfony) :
piedweb.twig.extension.render_attributes:
class: PiedWeb\RenderAttributes\TwigExtension
public: false
tags:
- { name: twig.extension }
Then use it :
{{ attr({class:"main content"})|raw }}
{{ mergeAttr({class:"main"}, {class:"content"})|raw }}
For Plates :
/* Template Init */ $templateEngine = new \League\Plates\Engine('app/views'); /* Load this extension */ $templateEngine->loadExtension(new \PiedWeb\RenderAttributes\PlatesExtension()); $this->render('test', ['attributes' => ['class' => 'content']]);
In your app/views/test.php
template file:
<?php $defaultAttributes = ['class' => 'main']; $attributes = isset($attributes) ? $this->mergeAttr($defaultAttributes, $attributes) : $defaultAttributes; ?> <div<?=$this->attr($attributes)?>>Hello World !</div>
Will render:
<div class="main content">Hello World !</div>
Installation
composer require piedweb/render-html-attributes
Requirements
Stand alone extension.
See composer.json
file.
Contributors
- Original author Robin (PiedWeb from the Alps Mountain)
- ...
License
MIT (see the LICENSE file for details)