<div class="ni_buttons">
    <div class="btngroup" data-neo-bn="container.buttons">
        {% set currentGroup = false %}
        {% set firstButton = true %}
        {% for item in items %}
            {% set type = item.getType() %}
            {% if type == 'blockType' %}
                {% if currentGroup %}
                    <li>
                        <a data-neo-bn="button.addBlock"
                           data-neo-bn-info="{{ item.getHandle() }}">
                            {{ item.getName() }}
                        </a>
                    </li>
                {% else %}
                    <div class="btn dashed{% if firstButton %} add icon{% endif %}"
                         data-neo-bn="button.addBlock"
                         data-neo-bn-info="{{ item.getHandle() }}">
                        {{ item.getName() }}
                    </div>
                    {% set firstButton = false %}
                {% endif %}
            {% elseif type == 'group' %}
                {% if currentGroup %}
                    {{ '</ul></div>' }}
                {% endif %}
                {% set currentGroup =
                    (item.isBlank()) or
                    ((loop.index + 2) > (items|length)) or
                    (items[loop.index + 1].getType() == 'group') ?
                        false :
                        item  %}
                {% if currentGroup %}
                    <div class="btn dashed{% if firstButton %} add icon{% endif %} menubtn"
                         data-neo-bn="button.group">
                        {{ item.getName() }}
                    </div>
                    {{ '<div class="menu"><ul>' }}
                    {% set firstButton = false %}
                {% endif %}
            {% endif %}
        {% endfor %}
        {% if currentGroup %}
            {{ '</ul></div>' }}
        {% endif %}
    </div>
    <div class="btn dashed add icon menubtn hidden"
         data-neo-bn="container.menu">
        {{ "Add a block"|t }}
    </div>
    <div class="menu">
        <ul>
        {% set currentGroup = false %}
        {% set lastGroupHadBlockTypes = false %}
        {% for item in items %}
            {% set type = item.getType() %}
            {% if type == 'blockType' %}
                {% if currentGroup and not lastGroupHadBlockTypes %}
                    {% set lastGroupHadBlockTypes = true %}
                    <h6>{{ currentGroup.getName() }}</h6>
                    {{ '<ul class="padded">' }}
                {% endif %}
                <li>
                    <a data-neo-bn="button.addBlock"
                       data-neo-bn-info="{{ item.getHandle() }}">
                        {{ item.getName() }}
                    </a>
                </li>
            {% elseif type == 'group' %}
                {% if not currentGroup or lastGroupHadBlockTypes %}
                    {{ '</ul>' }}
                {% endif %}
                {% set lastGroupHadBlockTypes = false %}
                {% set currentGroup = item.isBlank() ? false : item %}
                {% if not currentGroup %}
                    {{ '<ul>' }}
                {% endif %}
            {% endif %}
        {% endfor %}
        </ul>
    </div>
</div>
