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

Allow Nunjucks component params.attributes to support strings #4666

Closed
colinrotherham opened this issue Jan 22, 2024 · 1 comment · Fixed by #4742
Closed

Allow Nunjucks component params.attributes to support strings #4666

colinrotherham opened this issue Jan 22, 2024 · 1 comment · Fixed by #4742
Assignees
Labels
feature request User requests a new feature nunjucks

Comments

@colinrotherham
Copy link
Contributor

colinrotherham commented Jan 22, 2024

Related component

All components currently render Nunjucks attributes by looping object name/value pairs:

{%- for attribute, value in params.attributes %} {{ attribute }}="{{ value }}"{% endfor %}

This is unlike params.classes where string values are uses instead:

class="govuk-table__header {%- if item.classes %} {{ item.classes }}{% endif %}"

But this has prevented us from appending or merging new formGroup attributes to existing defaults in #4566 and in Create Password spikes #4512 (comment)

Context

This feature request comes from a by-design Nunjucks snag

Like in Jinja, object modification is not possible mozilla/nunjucks#307

Whilst looping object name/value pairs give us more flexibility (filtering, sorting), this means:

  • Individual attributes cannot be added
  • Individual attributes cannot be removed
  • Individual attributes cannot be reordered

This has been mentioned in the past (see #995, #1529, and mozilla/nunjucks#1455 etc)

The suggestion to support string values came from @36degrees in #4566 (comment)

Alternatives

Alternatively, we've tried a clever hack to push to Object.entries() arrays in Nunjucks for loops

But it's quite obscure and arrays lack named property access used by govukI18nAttributes() etc

Attributes as plain object

{{ govukButton({
  text: "Press me",
  attributes: {
    data-example: "value"
  }
}) }}

Attributes as Object.entries() array

{{ govukButton({
  text: "Press me",
  attributes: [
    ["data-example": "value"]
  ]
}) }}

Additional information (if applicable)

Supporting params.attributes as strings may simplify work on proposed attribute macros and we may want params.classes to work with string or object types too.

@colinrotherham
Copy link
Contributor Author

Up for review in #4742

@36degrees 36degrees moved this from Needs review 🔍 to Done 🏁 in GOV.UK Design System cycle board Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request User requests a new feature nunjucks
Projects
Development

Successfully merging a pull request may close this issue.

1 participant