For discussion: Import all components in template#1813
For discussion: Import all components in template#1813frankieroberto wants to merge 3 commits intomainfrom
Conversation
This would allow anyone using the exported template (for example, in a prototype or static site build) to not have to remember to import all the components individually.
|
I love this idea and it's definitely a good responsibility for NHS.UK frontend I do wonder if Avoiding conflictsWe'd probably want to preserve the last inherited approach we have to avoid conflicts:
{% from "app/components/button/macro.njk" import button %}
{% from "govuk/components/button/macro.njk" import govukButton %}
{% from "nhsuk/components/button/macro.njk" import button as nhsukButton %} |
|
@colinrotherham I’m not yet convinced we need to add an There’s probably an argument that we should have followed govuk-frontend in prefixing our components, but we are where we are and I don’t think it’s caused a ton of problems? If you end up adding your own component which has the same name as an NHS one, you have 2 options:
either might be fine, depending on your context? |
Yeah agreed, I've put a big line through it What I mean is that this PR can force all our components to take priority, depending on load path order i.e. Your local Breaking or non-breakingWe can get this into v10.x by having two templates:
The prototype kit can use It could even have its own local Otherwise it's a breaking change |
|
I think user imports should probably take precedence over any nhs ones. |
|
We'd then have prototype-kit-template.njk extend that instead of That said, I’m not sure that adding the imports directly to |
Me neither, I wasn't 100% sure when I said "depending on load path order" above We do already have a slight issue, unrelated but similar Take If a service team has their own local {% from "components/file-upload/macro.njk" import fileUpload %}But in this PR's case, what about teams that already have I'm a bit rusty but I can't remember the precedence across extended templates and/or includes |
|
Want to add it as |
|
@colinrotherham I’ve checked, and you can override imported components with your own components - the last-imported component seems to take precedence. So this for instance seems to work fine: {% extends "prototype-kit-template.njk" %}
{# Override NHS button component with a custom one #}
{% from '../components/button/macro.njk' import button %} |
|
The {% extends "prototype-kit-template.njk" %}
{% This does not seem to work: #}
{% from '../components/app_button/macro.njk' import appButton as button %}Not sure if that’s a Nunjucks bug! |
|



This updates the main
template.njkfile, which is exported in thenhsuk-frontendpackage, so that it imports all of the components available, rather than just the small number used in that template file.The main advantage of this would be that users of NHS frontend no longer have to remember to import the components, either within individual view files, or within their own template.
For the NHS prototype kit, that currently imports all components in
prototype-kit-template.njk. However, current if a minor version of an NHS frontend release introduces a new component, then we have to remember to update the template in the prototype kit to add the new import, and then release a new version of the kit.If NHS Frontend includes the imports itself, then users of the prototype kit would be able to upgrade NHS frontend without needing to wait for an update of the prototype kit.
Importing all components by default could also allow us to drop the individual component imports from the code examples on the service manual in future:
This might help those prototyping by avoiding their prototype code being littered with unneeded import lines.
However we might not want to drop the import lines from the examples straight away, as it would take people a while to upgrade, and having unnecessary import lines is probably better than not having them and the examples breaking for users who haven’t yet upgraded?