-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add docs on using Nunjucks macros #67
Conversation
Deploy preview for govuk-frontend-docs-preview ready! Built with commit 09d5b0b https://deploy-preview-67--govuk-frontend-docs-preview.netlify.app |
I'd say "your application and any frameworks that support it must be written in JavaScript" isn't quite right. It's worse than that because, as far as I can tell, Nunjucks can only be used within the browser or within Node. Just using Javascript isn't enough. |
And surely this is about using Nunjucks templates and macros? Not just macros alone? This reads as though you could choose to use Nunjucks templates but not the macros. Which you can't AFAIK. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels like a great improvement 👏🏻
For example, to add the breadcrumbs component to your page, add the following code from the first example on the [breadcrumbs component page](https://design-system.service.gov.uk/components/breadcrumbs/): | ||
|
||
```javascript | ||
{% from "govuk/components/breadcrumbs/macro.njk" import govukBreadcrumbs %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we happy with the risk of this getting out of sync with the first example on the Breadcrumbs page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm good question. @hannalaakso - is it better to ask users to copy the code from the breadcrumbs page? I can't remember why we specifically included the code now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good catch @36degrees, I think we can ask users to just copy it from the Design System.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I've updated in 0526b83. The last commit failed so just need to check the new commit is ok.
|
||
To see the options for a component, select the **Nunjucks** tab of the component example on any Design System website page, then select **Nunjucks macro options**. | ||
|
||
There’s a different way to [change how the page template works](https://design-system.service.gov.uk/styles/page-template/#changing-template-content). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially read this as 'there is a second, alternative way to do this same thing' rather than 'configuring the page template works differently'.
Given this is closely related to configuring the page template, should we mention this in the 'Set up Nunjucks' as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah good points both. I'll solve both these issues at once by tweaking the language slightly, and moving it to the end of the 'Set up Nunjucks' section where I agree it's a better fit.
weight: 45 | ||
--- | ||
|
||
# Use Nunjucks macros |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should just be called 'Use Nunjucks' as it covers two things that work in different ways:
- Extending (and configuring) the Nunjucks page template (which is not a macro)
- Using the component macros
Introducing them as two distinct things then breaking them into their own sections might help with explaining the fact that e.g. configuring the page template works differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, and I'm going to rejig the intro to this page slightly based on this and @mgladdish's second comment above.
Thank you for the great feedback @mgladdish. I've iterated on the docs now, including updates from @36degrees's comments, so hopefully the new content is clearer. |
0526b83
to
ea3cfaa
Compare
}) | ||
``` | ||
|
||
`autoescape: true` makes the output render as text by default, to help prevent cross-site scripting (XSS) attacks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the default config option for autoescape – is it still worth including?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh thanks for pointing this out - we can probably leave this out then.
|
||
1. Make sure you installed Nunjucks when you [installed GOV.UK Frontend with npm](/installing-with-npm). | ||
|
||
2. Import the Nunjucks package at the top of your configuration file, for example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure 'configuration file' is the right term here… it's more likely to be your main application file, whatever that's called…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'Main application file' sounds good to me. We should probably document these terms in our style guide...
|
||
You do not need to follow this guidance to [use Nunjucks macros in the GOV.UK Prototype Kit](https://design-system.service.gov.uk/get-started/prototyping/#using-nunjucks-macros). | ||
|
||
## Set up Nunjucks and use the page template |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect in most cases, what the user does is going to look quite different to this, depending on what framework they're using. For example, if they're using express they might install express-nunjucks
; if they're using eleventy it already has nunjucks installed and support built in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm this is a good point. I wondered about adding something above the list, along the lines of 'You may need to set up Nunjucks differently depending on the framework you're using'? But I wonder how useful that is (because it doesn't help you if you're not sure what to do). What do you think?
|
||
`autoescape: true` makes the output render as text by default, to help prevent cross-site scripting (XSS) attacks. | ||
|
||
4. Use the GOV.UK Frontend template by adding the following at the top of your view file: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a step missing here which is where the configured nunjucks environment is actually hooked in to the view layer of whatever framework they're using.
Unfortunately, what that step looks like is going to depend a lot on what framework is being used.
|
||
To see the options for a component, select the **Nunjucks** tab of the component example on any Design System website page, then select **Nunjucks macro options**. | ||
|
||
You must sanitise any HTML you pass in to Nunjucks macros you’re using in your live application, to protect your website against cross-site scripting (XSS) attacks. Talk to your team’s security specialist about how to do this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @vanitabarrett's comment on #70 applies here as well – the chances that there's a clear 'security specialist' to go and talk to for any given team seem slim to me.
@36degrees @hannalaakso Updated in 250ad57 and ready for re-review! We've made it clearer that how you set up Nunjucks (generically) will depend on your environment, but our example is for users using Express. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of outstanding Express-related comments, but otherwise this is looking good 👍🏻
|
||
## Set up Nunjucks and use the page template | ||
|
||
How you set up Nunjucks will depend on your environment. You can do the following to set up Nunjucks if you're using [Express](https://expressjs.com/). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we tested this with a clean install of Express? I think at the very least Nunjucks needs to be configured to 'install itself' against the Express app (as per https://mozilla.github.io/nunjucks/api.html#configure)
|
||
```javascript | ||
nunjucks.configure([ | ||
"node_modules/govuk-frontend/" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this need the user's 'view' folder included as well?
Tweaked to focus on Frontend-specific steps following a chat with Ollie on Friday. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍🏻
e7234eb
to
09d5b0b
Compare
As part of Improving guidance for using Nunjucks docs, this creates a new page in GOV.UK Frontend docs about using Nunjucks macros.
It also updates a couple of links elsewhere in the docs to point to this new page.
Thanks to @hannalaakso for her help pairing to create this new documentation.