{% with id=widget.attrs.id %}
  <ul {% if id %} id="{{ id }}" {% endif %}
                  class="flags-elements{% if widget.attrs.class %} {{ widget.attrs.class }} {% endif %}">
    <button name="flags_select_all">Select all</button>
    {% for group, options, index in widget.optgroups %} {% if group %}
      <li>{{ group }}
      <ul {% if id %} id="{{ id }}_{{ index }}" {% endif %}>{% endif %}
    {% for option in options %}
      <li>{% include option.template_name with widget=option %}</li>
    {% endfor %}
    {% if group %}
      </ul>
      </li>
    {% endif %}
    {% endfor %}
  </ul>{% endwith %}
