Files
notifications-admin/app/templates/components/list.html
Chris Hill-Scott 9332f57f55 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.
2016-06-05 18:39:32 +01:00

38 lines
855 B
HTML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{% 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 %}