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

Add docs on using Nunjucks macros #67

Merged
merged 1 commit into from
Aug 20, 2020
Merged

Conversation

m-green
Copy link
Contributor

@m-green m-green commented Jul 22, 2020

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.

@m-green m-green added the documentation Improvements or additions to documentation label Jul 22, 2020
@m-green m-green self-assigned this Jul 22, 2020
@netlify
Copy link

netlify bot commented Jul 22, 2020

Deploy preview for govuk-frontend-docs-preview ready!

Built with commit 09d5b0b

https://deploy-preview-67--govuk-frontend-docs-preview.netlify.app

@mgladdish
Copy link

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.

@mgladdish
Copy link

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.

Copy link
Contributor

@36degrees 36degrees left a 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 %}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@m-green m-green Jul 31, 2020

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).
Copy link
Contributor

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@m-green
Copy link
Contributor Author

m-green commented Jul 29, 2020

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.

})
```

`autoescape: true` makes the output render as text by default, to help prevent cross-site scripting (XSS) attacks.
Copy link
Contributor

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?

Copy link
Contributor Author

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:
Copy link
Contributor

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…

Copy link
Contributor Author

@m-green m-green Aug 3, 2020

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
Copy link
Contributor

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.

Copy link
Contributor Author

@m-green m-green Aug 3, 2020

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:
Copy link
Contributor

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.
Copy link
Contributor

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.

@m-green
Copy link
Contributor Author

m-green commented Aug 11, 2020

@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.

Copy link
Contributor

@36degrees 36degrees left a 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/).
Copy link
Contributor

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/"
Copy link
Contributor

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?

@m-green
Copy link
Contributor Author

m-green commented Aug 17, 2020

Tweaked to focus on Frontend-specific steps following a chat with Ollie on Friday.

Copy link
Contributor

@36degrees 36degrees left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻

@m-green m-green merged commit cd0d8d6 into master Aug 20, 2020
@m-green m-green deleted the add_using_nunjucks_docs branch August 20, 2020 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants