{# https://gist.github.com/bearz/7394681 #}
{% macro bs_form(form, action, opts, method='post') -%}
{% from 'bs_form_body.htm' import bs_form_body with context -%}
{% if not opts.layout_classes -%}
    {% do opts.update({'layout_classes': layout_classes}) -%}
{% endif -%}
{% if not opts.class -%}
    {% do opts.update({'class': ''}) -%}
{% endif -%}
<div class="panel panel-default {{ opts.class }}">
    {% if opts.title is defined -%}
    <div class="panel-heading"><h3 class="panel-title">{{ opts.title }}</h3></div>
    {% endif -%}
    <div class="panel-body">
        {% if action != '' -%}
        {% if opts.is_ajax -%}
            {% set atts = {'data-url': action, 'class': 'ajax-form'} -%}
        {% else -%}
            {% set atts = {'action': action} -%}
        {% endif -%}
        {% do atts.update({
            'method': method,
            'enctype': "multipart/form-data",
            'role': 'form'
        }) -%}
        <form{{ flatatt(atts) }}>
        {% endif -%}
            {% if action == '' -%}
            {# DisplayText fields use tr/td #}
            <table class="table table-bordered table-responsive">
                <colgroup>
                    <col class="{{ opts.layout_classes.label }}">
                    <col class="{{ opts.layout_classes.field }}">
                </colgroup>
            {% endif -%}
            {{ bs_form_body(form, opts.layout_classes) }}
            {% if action == '' -%}
            </table>
            {% endif -%}
            {% if opts.submit_text is defined -%}
            <button type="submit" class="btn btn-primary">{{ opts.submit_text }}</button>
            {% endif -%}
            {% if caller -%}
                {{ caller() }}
            {% endif -%}
        {% if action != '' -%}
        </form>
        {% endif -%}
    </div>
</div>
{% endmacro -%}
{% if _render_ -%}
{{ bs_form(form, action, opts) }}
{% endif -%}
