Skip to content
Merged
Show file tree
Hide file tree
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
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,53 @@ app.set("views", [

The views are now available when calling `res.render('view-name')` from express.

#### Details Component
The `detailsComponent` macro can be used to display a details summary and text on a page. It can take three parameters:

`details`: An object containing the summary and text to display. The summary is required, but the text is optional.

`customSummary`: A string that can be used to override the summary in the details object.

`customText`: A string that can be used to override the text in the details object.

This allows the component to be reused multiple times on the same page with different text.

If `customSummary` or `customText` are not provided, the macro will use the values from the details object. If the details object does not contain a summary or text, it will default to an empty string.



**Example usage:**

Use default summary and text from details object set in `pages.${route}.details`:
```
{
"test-page": {
"details":{
"summary": Details Summary",
"text": "Details Text",
"custom-summary": "Custom Summary",
"custom-text": "Custom Text
}
}
}
```
In your view file:
```
{% from "partials/details-summary.html" import detailsComponent %}

{# Default details component #}
{{ detailsComponent(details) }}

{# Set Override arguments - optional #}
{% set overrideSummary = t('pages.test-page.details.custom-summary') %} {# This can also be an ordinary string #}
{% set overrideText = t('pages.test-page.details.custom-text') %} {# This can also be an ordinary string #}

{# Override just default summary #}
{{ detailsComponent(details, overrideSummary) }}

{# Override both defaultsummary and text #}
{{ detailsComponent(details, overrideSummary, overrideText) }}
```
#### HOF Application

When used in a hof application in conjunction with [express-partial-templates](https://github.com/UKHomeOffice/express-partial-templates) the contents of the views directory are added to `res.locals.partials`. These are added right to left so conflicting views are resolved from the left-most directory.
Expand Down
17 changes: 17 additions & 0 deletions controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ module.exports = class Controller extends BaseController {
serviceName: this.getServiceName(lookup, res.locals),
captionHeading: this.getCaptionHeading(route, lookup, res.locals),
warning: this.getWarning(route, lookup, res.locals),
details: this.getDetails(route, lookup, res.locals),
subHeading: this.getSubHeading(route, lookup, res.locals),
intro: this.getIntro(route, lookup, res.locals),
backLink: this.getBackLink(req, res),
Expand Down Expand Up @@ -170,6 +171,22 @@ module.exports = class Controller extends BaseController {
return lookup(`pages.${route}.warning`, locals);
}

getDetails(route, lookup, locals) {
const details = lookup(`pages.${route}.details`, locals);
if (details && typeof details === 'object') {
return details;
}
const summary = lookup(`pages.${route}.details.summary`, locals);
const text = lookup(`pages.${route}.details.text`, locals);
if (summary || text) {
return {
summary,
text
};
}
return details;
}

getTitle(route, lookup, fields, locals) {
let fieldName = '';
if (_.size(fields)) {
Expand Down
19 changes: 11 additions & 8 deletions frontend/template-partials/views/partials/details-summary.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<details>
<summary role="button">
<span class="summary">{{summary}}</span>
</summary>
<div class="panel-indent">
{{details}}
</div>
</details>
{% from "govuk/components/details/macro.njk" import govukDetails %}

{% macro detailsComponent(details, customSummary=null, customText=null) %}
{% set summaryText = customSummary or details.summary or '' %}
{% set text = customText or details.text or '' %}

{{ govukDetails({
summaryText: summaryText,
text: text
}) }}
{% endmacro %}
2 changes: 1 addition & 1 deletion sandbox/apps/sandbox/views/form-guidance-link.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

{% block page_content %}
<h2>{{ t('pages.build-your-own-form.subheader') }}</h2>
<a class="govuk-link" href="https://ukhomeofficeforms.github.io/hof-guide/documentation/#building-your-first-hof-form">Link to HOF form guidance</a>
<a class="govuk-link" href="https://ukhomeofficeforms.github.io/hof-guide/documentation/#building-your-first-hof-form">Link to HOF form guidance</a>
{% endblock %}
Loading