-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added templating to html components and titles
- Loading branch information
Showing
26 changed files
with
328 additions
and
27 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
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,27 @@ | ||
# Templating | ||
|
||
If you find yourself faced with a form with content that needs to appear based on different field values, and there's a lot of options to choose from, putting all of this content in conditions may be at best very time-consuming, and at worst crash your form runner. | ||
|
||
With this in mind, templating may be a good solution for you. | ||
|
||
## How it works | ||
|
||
You can allow fields to be exposed to the additional context by adding an `exposeToContext` flag to the component's options. This can be done manually, or through the "expose to context" field in the designer. | ||
|
||
Once your field is exposed, the field can be used with additional context by adding the field in titles and html components, using nunjucks templating syntax, `{{ fieldName }}`. | ||
|
||
You can also access additional values (such as custom html) using the additional context file. This will allow you to populate different variables based of the same user input, and these can be accessed using the format `{{ additionalContexts.contextName[fieldName].variableName }}`. | ||
|
||
There is an example form set up to demonstrate this. If you start your runner, and navigate to http://localhost:3009/html-templating-example, you can choose either option and see the dynamic content on the next page. | ||
|
||
## Adding your own additional context | ||
|
||
You can add your own additional context by modifying the additional context json, located at `runner/src/server/templates/additionalContexts.json`. | ||
|
||
Once you have added your own additionalContext namespace in the same way the example namespace is populated, you'll be able to add your context variables to your html component as stated above e.g for the example form, the list is set by referencing `{{ additionalContexts.example[contentToDisplay].listItems }}`. | ||
|
||
### Using nunjucks filters | ||
|
||
You can also apply nunjucks filters to your context, for example you may want to add the `safe` filter for printing html content. You would do this the same way you would in normal nunjucks, e.g. `{{ additionalContexts.example[contentToDisplay].listItems | safe }}`. | ||
|
||
For more information about what nunjucks filters are available to you, [visit the Nunjucks docs](https://mozilla.github.io/nunjucks/templating.html#filters). |
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,17 @@ | ||
Feature: HTML templating in forms | ||
|
||
Scenario: Correct content should be shown for option one | ||
When the form "html-templating-example" exists | ||
And I choose "Answer 1" | ||
And I continue | ||
Then I see "This content is based on answer 1" | ||
And I see "Item 1" | ||
And I see "Item 2" | ||
|
||
Scenario: Correct content should be shown for option 2 | ||
When the form "html-templating-example" exists | ||
And I choose "Answer 2" | ||
And I continue | ||
Then I see "This content is based on answer 2" | ||
And I see "Item 3" | ||
And I see "Item 4" |
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,63 @@ | ||
{ | ||
"metadata": {}, | ||
"startPage": "/which-content-do-you-want-to-display", | ||
"pages": [ | ||
{ | ||
"title": "Which content do you want to display?", | ||
"path": "/which-content-do-you-want-to-display", | ||
"components": [ | ||
{ | ||
"name": "contentToDisplay", | ||
"options": { "exposeToContext": true }, | ||
"type": "RadiosField", | ||
"title": "Content to display", | ||
"list": "vYTQRu", | ||
"nameHasError": false, | ||
"values": { "type": "listRef" }, | ||
"schema": {} | ||
} | ||
], | ||
"next": [{ "path": "/second-page" }], | ||
"section": "gcdSFb" | ||
}, | ||
{ | ||
"path": "/second-page", | ||
"title": "Dynamic page based on your answers: {{ contentToDisplay }}", | ||
"components": [ | ||
{ | ||
"name": "azSyOn", | ||
"options": {}, | ||
"type": "Html", | ||
"content": "<p class=\"govuk-body\">This page demonstrates how templating works.</p>\n<p class=\"govuk-body\">Depending on the answer you chose for the first question, you should see different content displayed below.</p>\n<p class=\"govuk-body\">You chosen the option: {{contentToDisplay}}</p>\n{{additionalContexts.example[contentToDisplay].additionalInfo | safe}}\n<p class=\"govuk-body\">The following list will have different items</p>\n<ul class=\"govuk-list govuk-list--bullet\">\n{{additionalContexts.example[contentToDisplay].listItems | safe }}\n</ul>", | ||
"schema": {} | ||
} | ||
], | ||
"next": [{ "path": "/summary" }] | ||
}, | ||
{ | ||
"title": "Summary", | ||
"path": "/summary", | ||
"controller": "./pages/summary.js", | ||
"components": [], | ||
"next": [] | ||
} | ||
], | ||
"lists": [ | ||
{ | ||
"title": "Options", | ||
"name": "vYTQRu", | ||
"type": "string", | ||
"items": [ | ||
{ "text": "Answer 1", "value": "Answer 1" }, | ||
{ "text": "Answer 2", "value": "Answer 2" } | ||
] | ||
} | ||
], | ||
"sections": [{ "name": "gcdSFb", "title": "section" }], | ||
"conditions": [], | ||
"fees": [], | ||
"outputs": [], | ||
"version": 2, | ||
"skipSummary": false, | ||
"feeOptions": { "allowSubmissionWithoutPayment": true, "maxAttempts": 3 } | ||
} |
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
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,63 @@ | ||
{ | ||
"metadata": {}, | ||
"startPage": "/which-content-do-you-want-to-display", | ||
"pages": [ | ||
{ | ||
"title": "Which content do you want to display?", | ||
"path": "/which-content-do-you-want-to-display", | ||
"components": [ | ||
{ | ||
"name": "contentToDisplay", | ||
"options": { "exposeToContext": true }, | ||
"type": "RadiosField", | ||
"title": "Content to display", | ||
"list": "vYTQRu", | ||
"nameHasError": false, | ||
"values": { "type": "listRef" }, | ||
"schema": {} | ||
} | ||
], | ||
"next": [{ "path": "/second-page" }], | ||
"section": "gcdSFb" | ||
}, | ||
{ | ||
"path": "/second-page", | ||
"title": "Dynamic page based on your answers: {{ contentToDisplay }}", | ||
"components": [ | ||
{ | ||
"name": "azSyOn", | ||
"options": {}, | ||
"type": "Html", | ||
"content": "<p class=\"govuk-body\">This page demonstrates how templating works.</p>\n<p class=\"govuk-body\">Depending on the answer you chose for the first question, you should see different content displayed below.</p>\n<p class=\"govuk-body\">You chosen the option: {{contentToDisplay}}</p>\n{{additionalContexts.example[contentToDisplay].additionalInfo | safe}}\n<p class=\"govuk-body\">The following list will have different items</p>\n<ul class=\"govuk-list govuk-list--bullet\">\n{{additionalContexts.example[contentToDisplay].listItems | safe }}\n</ul>", | ||
"schema": {} | ||
} | ||
], | ||
"next": [{ "path": "/summary" }] | ||
}, | ||
{ | ||
"title": "Summary", | ||
"path": "/summary", | ||
"controller": "./pages/summary.js", | ||
"components": [], | ||
"next": [] | ||
} | ||
], | ||
"lists": [ | ||
{ | ||
"title": "Options", | ||
"name": "vYTQRu", | ||
"type": "string", | ||
"items": [ | ||
{ "text": "Answer 1", "value": "Answer 1" }, | ||
{ "text": "Answer 2", "value": "Answer 2" } | ||
] | ||
} | ||
], | ||
"sections": [{ "name": "gcdSFb", "title": "section" }], | ||
"conditions": [], | ||
"fees": [], | ||
"outputs": [], | ||
"version": 2, | ||
"skipSummary": false, | ||
"feeOptions": { "allowSubmissionWithoutPayment": true, "maxAttempts": 3 } | ||
} |
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
Oops, something went wrong.