-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
--- | ||
title: Use Nunjucks | ||
weight: 45 | ||
--- | ||
|
||
# Use Nunjucks | ||
|
||
You can use our Nunjucks code to generate the HTML for pages and components. | ||
|
||
You can also change the HTML that's generated by passing options into the Nunjucks code. | ||
|
||
If you use Nunjucks, your application will be easier to update when a new version of GOV.UK Frontend is released. | ||
|
||
To use Nunjucks, your application and any frameworks that support your application must use Node.js. | ||
|
||
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). | ||
|
||
## Before you start | ||
|
||
You must first: | ||
|
||
- [install GOV.UK Frontend with Node.js package manager (npm)](/installing-with-npm/) | ||
- make sure you’ve installed Nunjucks so it’s working in your environment | ||
|
||
## Set up Nunjucks and use the page template | ||
|
||
1. Add `node_modules/govuk-frontend/` to your list of Nunjucks paths, so Nunjucks knows where to find the GOV.UK Frontend template and components. | ||
|
||
For example: | ||
|
||
```javascript | ||
nunjucks.configure([ | ||
"node_modules/govuk-frontend/", | ||
"YOUR-VIEWS-FOLDER" | ||
]) | ||
``` | ||
|
||
2. Use the GOV.UK Frontend template by adding the following at the top of your view file: | ||
|
||
```javascript | ||
{% extends "govuk/template.njk" %} | ||
``` | ||
|
||
3. Go to the [default page template example](https://design-system.service.gov.uk/styles/page-template/#default) on the GOV.UK Design System website, then copy the Nunjucks code into your view file. | ||
|
||
You may need to change the paths in the Nunjucks code to [get the CSS, assets and JavaScript working](/importing-css-assets-and-javascript). | ||
|
||
Find out how to [change how the page template works](https://design-system.service.gov.uk/styles/page-template/#changing-template-content). | ||
|
||
## Adding a component | ||
|
||
Go to any component page on the Design System website, then copy the Nunjucks macro code from the **Nunjucks** tab of any example. | ||
|
||
For example, to add the breadcrumbs component to your page, copy the code from the **Nunjucks** tab in the first example on the [breadcrumbs component page](https://design-system.service.gov.uk/components/breadcrumbs/). | ||
|
||
## Changing a component | ||
|
||
You can use options to change how a component looks or behaves. | ||
|
||
For example, use the `text` option to change the text on a [button](https://design-system.service.gov.uk/components/button/): | ||
|
||
```javascript | ||
{{ govukButton({ | ||
text: "Example button text" | ||
}) }} | ||
``` | ||
|
||
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. You can read more about [XSS](https://developer.mozilla.org/en-US/docs/Web/Security/Types_of_attacks#Cross-site_scripting_XSS) on the MDN website. |