Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Attribute Modifier #9796

Open
wants to merge 10 commits into
base: 4.x
Choose a base branch
from

Conversation

potsky
Copy link

@potsky potsky commented Mar 27, 2024

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

@marcorieser
Copy link
Contributor

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

@edalzell
Copy link
Contributor

Love this

@potsky
Copy link
Author

potsky commented Mar 27, 2024

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!

@marcorieser
Copy link
Contributor

Do you mean this?

exactly.

@potsky
Copy link
Author

potsky commented Mar 27, 2024

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

@marcorieser
Copy link
Contributor

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

@potsky
Copy link
Author

potsky commented Mar 27, 2024

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

Make sense. Done ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants