{% set ns = namespace(left=0, right=0) %} {% for i in range(20) %} {% if uniquePathsAt(elements, '1-' ~ i ~ '$') | length != 0 %} {% set ns.left = i %} {% endif %} {% if uniquePathsAt(elements, '2-' ~ i ~ '$') | length != 0 %} {% set ns.right = i %} {% endif %} {% endfor %} {% macro item_left(index, count) -%} {% set id = '1-'~index %} {% if index == count %} {% else %} {% endif %} {% if index == count %} {% endif %} {% if index == count %} {% endif %} {{index}} {%- endmacro %} {% macro item_right(index, count) -%} {% set id = '2-'~index %} {% if index == count %} {% else %} {% endif %} {% if index == count %} {% endif %} {% if index == count %} {% endif %} {{index}} {%- endmacro %} {% for i in range(ns.left) %} {{ item_left(i + 1, ns.left) }} {% endfor %} {% for i in range(ns.right) %} {{ item_right(i + 1, ns.right) }} {% endfor %}