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

[5.x] Attribute Modifier #9796

Merged
merged 14 commits into from
May 21, 2024
Merged

Conversation

potsky
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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 ✅

@duncanmcclean duncanmcclean changed the base branch from 4.x to 5.x May 14, 2024 14:17
@duncanmcclean duncanmcclean changed the title [4.x] Attribute Modifier [5.x] Attribute Modifier May 14, 2024
Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I made just a few tweaks:

  • Support for collections
  • When an object doesn't have a __toString, let the exception be thrown.
  • Simplify tests

@jasonvarga jasonvarga merged commit 73c48a4 into statamic:5.x May 21, 2024
16 checks passed
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.

5 participants