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

Introduce Nunjucks macros abstracting data attribute rendering #2917

Closed
2 tasks
romaricpascal opened this issue Oct 13, 2022 · 3 comments · Fixed by #4743
Closed
2 tasks

Introduce Nunjucks macros abstracting data attribute rendering #2917

romaricpascal opened this issue Oct 13, 2022 · 3 comments · Fixed by #4743
Labels
feature request User requests a new feature nunjucks tooling

Comments

@romaricpascal
Copy link
Member

romaricpascal commented Oct 13, 2022

What

Abstract rendering of data-attributes for passing configuration into a govukDataAttributes with a call of the following shape:

{{
	govukDataAttributes({
	   'attribute-name': value,
	   'other-attribute-name': {
		  value: otherValue, 
	      testUndefined: true,
	      escape: false # Only if there are instances we don't escape the attributes
	   }
	})
}}

Expand the concept with a helper for internationalisation attributes that relies on plurals or string key instead of value and delegates to govukDataAttributes, building the i18n.<attribute-name>[.plural-category] attribute name. Based on the ideas in this comment.

Why

Setting the attributes is very repetitive in the templates and always the same shape:

  1. if {{value}} or {{value !== undefined}} to condition the rendering of the attribute itself
  2. output " data-{{name}}="{{value | escape}}"

Who needs to work on this

Developers

Who needs to review this

Developers

Done when

  • Abstraction macros have been introduced and tested
  • Component templates have been updated to use the abstractions
@querkmachine
Copy link
Member

I notice that an i18n related macro was added a couple of months ago, that seems related to this idea.

Is this issue still relevant or in need of updates in response to that?

@romaricpascal
Copy link
Member Author

romaricpascal commented Apr 27, 2023

That macro is related to this idea indeed. It proposed to expand on it to set up a macro for rendering attributes in general, as we often do the same thing for rendering them: check if they're undefined, escape them...

Under the hood, the i18n macro would likely delegate to this generic macro for outputting its attribute, allowing it to focus on the bits specific to i18n (fishing the plural forms and formatting the attribute names).

The issue remains relevant I think. It doesn't need tackling urgently though (and is open to counter arguments if we think it's unhelpful 😊)

@colinrotherham
Copy link
Contributor

Hadn't realised this issue existed. We nearly implemented this in:

Fairly quick to pull out another macro (for data- or regular attributes in general) from govukI18nAttributes()

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 tooling
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants