Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions docs/features/code-based/page-controllers.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,28 @@ Both are imported from `@defra/forms-engine-plugin/controllers/<ClassName>.js`.

## Examples

- [Changing the Nunjucks template](#changing-the-nunjucks-template)
- [Fetching data for the view model](#fetching-data-for-the-view-model)
- [Intercepting the GET handler](#intercepting-the-get-handler)
- [Writing to state on POST](#writing-to-state-on-post)
- [Display-only page (no form components)](#display-only-page-no-form-components)

### Changing the Nunjucks template

To render a different Nunjucks template without changing any handler logic, declare `viewName` as a class property. No handler overrides are needed — all standard GET and POST behaviour (validation, state, routing, error display) runs unchanged:

```ts
import { QuestionPageController } from '@defra/forms-engine-plugin/controllers/QuestionPageController.js'

class CustomViewPageController extends QuestionPageController {
viewName = 'my-custom-page'
}
```

The template receives the same view model as the default `index` template. See [Templates and views](./page-views.md) to configure Nunjucks to resolve your template file.

> **Note:** A `view` property on the page definition in your form JSON also overrides `viewName` — but that approach does not require a custom controller at all. Use the class property when you want every page using this controller to share the same template.

### Fetching data for the view model

Override `makeGetRouteHandler()` to fetch data before the page renders and pass it to your Nunjucks template. Call `this.getViewModel()` to build the standard model, then spread in your additional data:
Expand Down
Loading