backdrop-dev / attr
Backdrop Attr provides devs a system for adding filterable attributes. This is sort of like `body_class()`, `post_class()`, and `comment_class()` on steroids.
Requires
- php: >=7.4
- backdrop-dev/framework: ^2.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-17 21:09:03 UTC
README
Backdrop Attr builds filterable HTML attributes for any element. It is like body_class(), post_class() and comment_class(), but for any element you want, and every attribute can be changed with filters.
Requirements
- PHP 7.4 or later
- backdrop-dev/framework ^2.0
Installation
composer require backdrop-dev/attr
Register the provider when you boot your application:
$app->provider( \Backdrop\Attr\Provider::class );
Usage
<body <?php Backdrop\Attr\display( 'body' ); ?>> <article <?php Backdrop\Attr\display( 'post' ); ?>> <aside <?php Backdrop\Attr\display( 'sidebar', 'primary', [ 'role' => 'complementary' ] ); ?>>
display( $name, $context = '', $attr = [] )outputs the escaped attributes.render( $name, $context = '', $attr = [] )returns them as a string.attr( $name, $context = '', $attr = [] )returns the object, which supportsget( 'id' )andwith( 'post', $post_id ).
Attribute values: a string or number is escaped and output, true outputs a bare attribute such as hidden, and false or null leaves the attribute out.
Without a custom class, the class is {name} {name}--{context}, for example sidebar sidebar--primary. The names html, body, post, entry and comment use the matching WordPress attributes and classes.
Filters
backdrop/attr/{name}/defaultsfilters the defaults before your input is merged in.backdrop/attrandbackdrop/attr/{name}filter the final array of attributes.backdrop/attr/{name}/classfilters the classes as an array.
Tests
php tests/run.php
License
GPL-2.0-or-later