Download the PHP package stillat/statamic-attribute-renderer without Composer
On this page you can find all versions of the php package stillat/statamic-attribute-renderer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package statamic-attribute-renderer
Attribute Renderer for Statamic
Attribute Renderer is a utility addon that helps create HTML attribute strings from arrays.
At a high level, it allows you to convert something like this:
to the following HTML attribute string:
The attributes renderer is also context aware, and can do things like this:
These examples are basic, and Attribute Renderer supports even more complex scenarios.
How to Install
Attribute Renderer can be installed by running the following command from the root of your project:
Converting Arrays to Attribute Strings
The simplest way to convert a key/value array of attribute details to a string is using the attributes
utility function:
which produces the following result:
Resolving Variable Values
We can resolve variables from contextual data, which is supplied as the second argument to the attributes
function. When specifying variable names, we simply prefix them with the $
symbol:
We can use $$
to escape the beginning of a variable string to emit string beginning with a single $
:
which produces the following output:
Attribute Renderer does not support more complicated variable paths, such as nested properties, or array indices. If you need something more complicated, consider using a closure based variable resolver.
Closure Based Variable Resolvers
We can supply a Closure
as the value of our attribute in order to resolve more complicated values. We will receive the context array as the first argument:
which produces:
Skippable/Ignorable Properties
By default, Attribute Renderer will emit empty strings if a value returns null
:
produces:
we can let Attribute Renderer know its okay to ignore a property when producing the final result:
would now produce:
However, if the value did exist within the context:
the ignorable property is added to the output:
Rejectable Properties
Rejectable properties are similar to ignorable properties. However, if a null
or empty string value is resolved for one of these values, an empty attribute string is returned, regardless of if other property values were matched:
License
Attribute Renderer is free software, released under the MIT license.