cms
cms copied to clipboard
[5.x] Attribute Modifier
This PR adds a new modifier attribute to render an HTML attribute for example when the given $value is not empty (related to this discussion https://github.com/statamic/cms/discussions/9323)
The main goal is to avoid this:
<a href="https://statamic.awesome"
{{ if class }}class="{{ class }}"{{ /if }}
{{ if target }}target="{{ target }}"{{ /if }}
>Go to Statamic</a>
and to use this instead:
<a href="https://statamic.awesome"
{{ class | attribute:class }}
{{ target | attribute:target }}
>Go to Statamic</a>
It supports:
- boolean (only returns the attribute if true, think about the
requiredattribute in an input) - objects (only if they implement the
__toString()method) - array (render json content if array is not empty)
- int
- float
- string
Does this work for attributes without a value e.g. download or required?
Love this
Does this work for attributes without a value e.g.
downloadorrequired?
Do you mean this?
<input type=... {{ mandatory | attribute:required }}>
should returns
<input type=... required>
if mandatory is true and
<input type=...>
if mandatory is false?
For the moment, if mandatory is true, it returns
<input type=... required="1">
I will update this right now, this is a very good idea, thank you!
Do you mean this?
exactly.
✅ Done. It supports boolean, objects, int, float, stringable objects too Array and non stringable objects will return nothing.
Array could be converted to json. Just an idea. Thinking about x-data or some other data- attribute.
Array could be converted to json. Just an idea. Thinking about
x-dataor some otherdata-attribute.
Make sense. Done ✅