diff --git a/app/templates/components/list.html b/app/templates/components/list.html new file mode 100644 index 000000000..26eb8e6ee --- /dev/null +++ b/app/templates/components/list.html @@ -0,0 +1,37 @@ +{% macro formatted_list( + items, + conjunction='and', + before_each='‘', + after_each='’', + separator=', ', + prefix='', + prefix_plural='' +) %} + {% if items|length == 1 %} + {{ prefix }} {{ before_each|safe }}{{ (items|list)[0] }}{{ after_each|safe }} + {% elif items %} + {{ prefix_plural }} + {% for item in (items|list)[0:-1] -%} + {{ before_each|safe -}} + {{ item -}} + {{ after_each|safe -}} + {% if not loop.last -%} + {{ separator -}} + {% endif -%} + {% endfor %} + {{ conjunction }} + {{ before_each|safe -}} + {{ (items|list)[-1] -}} + {{ after_each|safe }} + {%- endif %} +{%- endmacro %} + + +{% macro list_of_placeholders(placeholders) %} + {{ formatted_list( + placeholders, + before_each="((", + after_each='))', + separator=' ' + ) }} +{%- endmacro %} diff --git a/app/templates/views/check.html b/app/templates/views/check.html index 1eec4a52c..e36d913fb 100644 --- a/app/templates/views/check.html +++ b/app/templates/views/check.html @@ -6,20 +6,7 @@ {% from "components/placeholder.html" import placeholder %} {% from "components/file-upload.html" import file_upload %} {% from "components/page-footer.html" import page_footer %} - -{% macro list_of_columns(columns, conjunction='and') %} - {% if columns|length == 1 %} - ‘{{ (columns|list)[0] }}’ - {% else %} - {{ - "‘{}’ {} ‘{}’".format( - "’, ‘".join((columns|list)[0:-1]), - conjunction, - (columns|list)[-1] - ) - }} - {%- endif %} -{%- endmacro %} +{% from "components/list.html" import formatted_list %} {% block page_title %} {{ page_heading if errors else "Check and confirm" }} – GOV.UK Notify @@ -53,16 +40,19 @@ your template
- Your file has columns called {{ list_of_columns(recipients.column_headers) }}. + Your file has {{ formatted_list( + recipients.column_headers, + prefix='one column, called', + prefix_plural='columns called' + ) }}.
- It doesn’t have - {% if recipients.missing_column_headers|length == 1 %} - a column - {% else %} - columns - {% endif %} - called {{ list_of_columns(recipients.missing_column_headers, 'or') }}. + It doesn’t have {{ formatted_list( + recipients.missing_column_headers, + conjunction='or', + prefix='a column called', + prefix_plural='columns called' + ) }}.
{% endcall %} diff --git a/app/templates/views/styleguide.html b/app/templates/views/styleguide.html index 59859b151..5a5ce64e4 100644 --- a/app/templates/views/styleguide.html +++ b/app/templates/views/styleguide.html @@ -10,6 +10,7 @@ {% from "components/textbox.html" import textbox %} {% from "components/file-upload.html" import file_upload %} {% from "components/api-key.html" import api_key %} +{% from "components/list.html" import formatted_list %} {% block page_title %} Styleguide – GOV.UK Notify @@ -230,4 +231,23 @@ {{ api_key('d30512af92e1386d63b90e5973b49a10') }} + ++ {{ formatted_list('A', prefix="one item called") }} +
+ ++ {{ formatted_list('AB', prefix_plural="two items called") }} +
+ ++ {{ formatted_list('ABC') }} +
+ +
+ {{ formatted_list('ABCD', before_each='', after_each='', conjunction='or') }}
+
- You removed - {{ list_of_placeholders(template_change.placeholders_removed) }} + You removed {{ list_of_placeholders(template_change.placeholders_removed) }}
{% endif %} {% if template_change.placeholders_added %}