piedweb/render-html-attributes

[Twig Extension] Transform an array in html tag attributes.

0.1.837 2025-02-03 18:06 UTC

This package is auto-updated.

Last update: 2025-02-03 18:10:03 UTC


README

Latest Version Software License Code Coverage Type Coverage Total Downloads

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 attributes
  • mergeAttr($attributes1, $attributes2, [$attributes3, ...]) merge multiple array without loosing values (Eg. : ['class' => 'main'] + ['class' => 'content'] = ['class' => 'main content'])

Table of contents

Usage

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 }}

Installation

composer require piedweb/render-html-attributes

Requirements

Stand alone extension.

See composer.json file.

Contributors

License

MIT (see the LICENSE file for details)

Open Source Package