cms icon indicating copy to clipboard operation
cms copied to clipboard

[5.x] Attribute Modifier

Open potsky opened this issue 1 year ago • 7 comments

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

potsky avatar Mar 27 '24 10:03 potsky

Does this work for attributes without a value e.g. download or required?

marcorieser avatar Mar 27 '24 10:03 marcorieser

Love this

edalzell avatar Mar 27 '24 15:03 edalzell

Does this work for attributes without a value e.g. download or required?

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!

potsky avatar Mar 27 '24 15:03 potsky

Do you mean this?

exactly.

marcorieser avatar Mar 27 '24 16:03 marcorieser

✅ Done. It supports boolean, objects, int, float, stringable objects too Array and non stringable objects will return nothing.

potsky avatar Mar 27 '24 16:03 potsky

Array could be converted to json. Just an idea. Thinking about x-data or some other data- attribute.

marcorieser avatar Mar 27 '24 16:03 marcorieser

Array could be converted to json. Just an idea. Thinking about x-data or some other data- attribute.

Make sense. Done ✅

potsky avatar Mar 27 '24 19:03 potsky