Skip to content

Commit 8c81a25

Browse files
committed
Move base layout into nunjucks options
1 parent 1613637 commit 8c81a25

6 files changed

Lines changed: 46 additions & 5 deletions

File tree

docs/GETTING_STARTED.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,44 @@ await server.register({
9292
}
9393
})
9494

95+
const viewPaths = [join(config.get('appDir'), 'views')]
96+
9597
// Register the `forms-engine-plugin`
9698
await server.register({
97-
plugin
99+
plugin,
100+
options: {
101+
cacheName: 'session', // must match a session you've instantiated in your hapi server config
102+
/**
103+
* Options that DXT uses to render Nunjucks templates
104+
*/
105+
nunjucks: {
106+
basePageLayout: 'your-base-layout.html', // the base page layout. Usually based off https://design-system.service.gov.uk/styles/page-template/
107+
viewPaths // list of directories DXT should use to render your views. Must contain basePageLayout.
108+
},
109+
/**
110+
* Services is what DXT uses to interact with external APIs
111+
*/
112+
services: {
113+
formsService, // where your forms should be downloaded from.
114+
formSubmissionService, // handles temporary storage of file uploads
115+
outputService // where your form should be submitted to
116+
},
117+
/**
118+
* View context attributes made available to your pages. Returns an object containing an arbitrary set of key-value pairs.
119+
*/
120+
viewContext: (request) => {
121+
"example": "hello world" // available to render on a nunjucks page as {{ example }}
122+
}
123+
}
98124
})
99125

100126
await server.start()
101127
```
102128

103129
## Step 3: Handling static assets
104130

131+
TODO: CSS will be updated with a proper build process using SASS.
132+
105133
1. [Update webpack to bundle the DXT application assets (CSS, JavaScript, etc)](https://github.com/DEFRA/forms-engine-plugin-example-ui/pull/1/files#diff-1fb26bc12ac780c7ad7325730ed09fc4c2c3d757c276c3dacc44bfe20faf166f)
106134
2. [Serve the newly bundled assets from your web server](https://github.com/DEFRA/forms-engine-plugin-example-ui/pull/1/files#diff-e5b183306056f90c7f606b526dbc0d0b7e17bccd703945703a0811b6e6bb3503)
107135

src/server/plugins/engine/configureEnginePlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const configureEnginePlugin = async ({
4141
controllers,
4242
cacheName: 'session',
4343
nunjucks: {
44+
baseLayoutPath: 'dxt-devtool-baselayout.html',
4445
paths: [join(findPackageRoot(), 'src/server/devserver')] // custom layout to make it really clear this is not the same as the runner
4546
},
4647
viewContext: devtoolContext

src/server/plugins/engine/plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ export interface PluginOptions {
9292
filters?: Record<string, FilterFunction>
9393
pluginPath?: string
9494
nunjucks: {
95+
baseLayoutPath: string
9596
paths: string[]
9697
}
9798
viewContext: (
9899
request: FormRequest | FormRequestPayload | null
99-
) => Record<string, unknown> & { baseLayoutPath?: string }
100+
) => Record<string, unknown>
100101
}
101102

102103
export const plugin = {
@@ -171,6 +172,7 @@ export const plugin = {
171172
}
172173
})
173174

175+
server.expose('baseLayoutPath', nunjucksOptions.baseLayoutPath)
174176
server.expose('viewContext', viewContext)
175177
server.expose('cacheService', cacheService)
176178

src/server/plugins/nunjucks/context.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,23 @@ export function context(request) {
3434
const pluginStorage = request?.server.plugins['forms-engine-plugin']
3535
let consumerViewContext = {}
3636

37-
if (pluginStorage && 'viewContext' in pluginStorage) {
37+
if (!pluginStorage) {
38+
throw Error('context called before plugin registered')
39+
}
40+
41+
if (!pluginStorage.baseLayoutPath) {
42+
throw Error('Missing baseLayoutPath in plugin.options.nunjucks')
43+
}
44+
45+
if ('viewContext' in pluginStorage) {
3846
consumerViewContext = pluginStorage.viewContext(request)
3947
}
4048

4149
/** @type {ViewContext} */
4250
const ctx = {
4351
// take consumers props first so we can override it
4452
...consumerViewContext,
53+
baseLayoutPath: pluginStorage.baseLayoutPath,
4554
appVersion: pkg.version,
4655
config: {
4756
cdpEnvironment: config.get('cdpEnvironment'),
@@ -52,7 +61,7 @@ export function context(request) {
5261
serviceVersion: config.get('serviceVersion')
5362
},
5463
crumb: safeGenerateCrumb(request),
55-
currentPath: request ? `${request.path}${request.url.search}` : undefined,
64+
currentPath: `${request.path}${request.url.search}`,
5665
previewMode: isPreviewMode ? params?.state : undefined,
5766
slug: isResponseOK ? params?.slug : undefined
5867
}
@@ -76,7 +85,6 @@ export function devtoolContext() {
7685
}
7786

7887
return {
79-
baseLayoutPath: 'dxt-devtool-baselayout.html', // from plugin.options.nunjucks.paths
8088
assetPath: '/assets',
8189
getDxtAssetPath: (asset = '') => {
8290
return `/${webpackManifest?.[asset] ?? asset}`

src/server/plugins/nunjucks/types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/**
1313
* @typedef {object} ViewContext - Nunjucks view context
1414
* @property {string} appVersion - Application version
15+
* @property {string} [baseLayoutPath] - Base layout path
1516
* @property {Partial<Config>} config - Application config properties
1617
* @property {string} [crumb] - Cross-Site Request Forgery (CSRF) token
1718
* @property {string} [cspNonce] - Content Security Policy (CSP) nonce

src/typings/hapi/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ declare module '@hapi/hapi' {
2020
generate?: (request: Request | FormRequest | FormRequestPayload) => string
2121
}
2222
'forms-engine-plugin': {
23+
baseLayoutPath: string
2324
cacheService: CacheService
2425
viewContext: context
2526
}

0 commit comments

Comments
 (0)