{%- macro m_field(field, horizontal=False, label_width=2, field_width=8) %}
  {%- set with_label = kwargs.pop('with_label', not field.flags.hidden) %}
  {%- set placeholder = '' %}
  {%- if not with_label %}
    {%- set placeholder = field.label.text %}
  {%- endif %}
  {%- set css_class = kwargs.pop('class_', '') %}
  {%- set standard_field = field.type not in ('CSRFTokenField',) %}
  {%-  if standard_field and field.type not in ('BooleanField',) %}
    {%- set css_class =  'form-control ' + css_class %}
  {%- endif %}
  {%- if field.flags.required %}
    {%- set css_class = css_class + ' required' %}
  {%- endif %}
  <div class="form-group{% if field.errors and not field.form %} has-error{% endif %}{% if field.flags.required %} required{% endif %}">
    {%- if standard_field and not horizontal %}
      <div class="col-xs-12">
    {%- endif %}
    {%- if with_label and not (field.is_hidden or field.type in ('CSRFTokenField', 'HiddenField')) %}
      <label class="control-label {%- if horizontal %} col-sm-{{ label_width }} {%- endif %}" for="{{ field.id }}">{{ field.label.text }}</label>
    {%- endif %}

    {%- if horizontal and standard_field %}
      <div class="col-sm-{{ field_width }}">
    {%- endif %}

    {%- if field.type == 'CSRFTokenField' %}
      {{ field() }}

    {%- elif field.type == 'BooleanField' %}
      <label class="checkbox-inline">
        {# the 'nbsp' after the field is here because without any text bootstrap
           misalign the checkbox :-( #}
          {{ field(class_=css_class, **kwargs) }} 
      </label>

    {%- else %}
      {%- if field.type in ('TextField', 'TextAreaField', 'PasswordField') %}
        {%- set css_class = css_class + ' input-xlarge' %}
      {%- elif field.type == 'FileField' %}
        {%- set css_class = css_class + ' input-file' %}
      {%- endif %}
        {{ field(class_=css_class, placeholder=placeholder, **kwargs)|safe }}
    {%- endif %}

    {%- if field.errors and not field.form %}
      <span class="help-inline text-danger">{{ field.errors|join('<br />'|safe) }}</span>
    {%- endif %}

    {%- if field.description %}
      <span class="help-block">{{ field.description|safe }}</span>
    {%- endif %}

    {%- if standard_field %}
      </div> {# when horizontal: close input div; else closes div.col-xs-12 before label #}
    {%- endif %}
  </div> {# form-group #}
{%- endmacro %}

{%- macro m_form(form, fields=(), action="", method="POST", enctype="multipart/form-data", 
                 horizontal=False, id=None, role="form",
                 label_width=2, field_width=8) %}
  <form action="{{ action }}" method="{{ method }}" enctype="{{ enctype }}" {%- if horizontal %}class="form-horizontal" {%- endif %} {%- if id %} id="{{ id }}" {%- endif %} role="{{ role }}">

    {%- for field in (fields or form) %}
      {{ m_field(field, horizontal=horizontal, label_width=label_width, field_width=field_width) }}
    {%- endfor %}

    <div class="form-actions">
      {%- for button in g.view.buttons %}
        {{ button.render() }}
      {%- endfor %}
    </div>

  </form>
{%- endmacro %}
