Make components for nicely formatted lists

We have a couple of places now where we want nice lists made from `list`s, eg

- ‘name’, ‘date’ and ‘phone number’
- ((firstname)) ((lastname)) or ((date))

This commit adds a more generic component for doing this, which can handle:

- 1, 2, and n items
- comma (or other character) separated lists
- a conjunction between the last and one-before-last item
- characters to be inserted before and after each item, eg an opening and
  closing HTML tag

It also pulls the `list_of_placeholders` component from the breaking change
page, and makes it use the `formatted_list` component under the hood.
This commit is contained in:
Chris Hill-Scott
2016-06-03 14:30:17 +01:00
parent 3ace856d74
commit 9332f57f55
4 changed files with 71 additions and 31 deletions

View File

@@ -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="<span class='placeholder'>((",
after_each='))</span>',
separator=' '
) }}
{%- endmacro %}