Git based wiki inspired by Gollum
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.0KB

  1. {# Source: https://gist.github.com/bearz/7394681 #}
  2. {# Renders field for bootstrap 3 standards.
  3. Params:
  4. field - WTForm field
  5. kwargs - pass any arguments you want in order to put them into the html attributes.
  6. There are few exceptions: for - for_, class - class_, class__ - class_
  7. Example usage:
  8. {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
  9. #}
  10. {% macro render_field(field, label_visible=true) -%}
  11. <div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
  12. {% if (field.type != 'HiddenField' or field.type !='CSRFTokenField') and label_visible %}
  13. <label for="{{ field.id }}" class="control-label">{{ field.label }}</label>
  14. {% endif %}
  15. {{ field(class_='form-control', **kwargs) }}
  16. {% if field.errors %}
  17. {% for e in field.errors %}
  18. <p class="help-block">{{ e }}</p>
  19. {% endfor %}
  20. {% endif %}
  21. </div>
  22. {%- endmacro %}
  23. {# Renders checkbox fields since they are represented differently in bootstrap
  24. Params:
  25. field - WTForm field (there are no check, but you should put here only BooleanField.
  26. kwargs - pass any arguments you want in order to put them into the html attributes.
  27. There are few exceptions: for - for_, class - class_, class__ - class_
  28. Example usage:
  29. {{ macros.render_checkbox_field(form.remember_me) }}
  30. #}
  31. {% macro render_checkbox_field(field) -%}
  32. <div class="checkbox">
  33. <label>
  34. {{ field(type='checkbox', **kwargs) }} {{ field.label }}
  35. </label>
  36. </div>
  37. {%- endmacro %}
  38. {# Renders radio field
  39. Params:
  40. field - WTForm field (there are no check, but you should put here only BooleanField.
  41. kwargs - pass any arguments you want in order to put them into the html attributes.
  42. There are few exceptions: for - for_, class - class_, class__ - class_
  43. Example usage:
  44. {{ macros.render_radio_field(form.answers) }}
  45. #}
  46. {% macro render_radio_field(field) -%}
  47. {% for value, label, _ in field.iter_choices() %}
  48. <div class="radio">
  49. <label>
  50. <input type="radio" name="{{ field.id }}" id="{{ field.id }}" value="{{ value }}">{{ label }}
  51. </label>
  52. </div>
  53. {% endfor %}
  54. {%- endmacro %}
  55. {# Renders WTForm in bootstrap way. There are two ways to call function:
  56. - as macros: it will render all field forms using cycle to iterate over them
  57. - as call: it will insert form fields as you specify:
  58. e.g. {% call macros.render_form(form, action_url=url_for('login_view'), action_text='Login',
  59. class_='login-form') %}
  60. {{ macros.render_field(form.email, placeholder='Input email', type='email') }}
  61. {{ macros.render_field(form.password, placeholder='Input password', type='password') }}
  62. {{ macros.render_checkbox_field(form.remember_me, type='checkbox') }}
  63. {% endcall %}
  64. Params:
  65. form - WTForm class
  66. action_url - url where to submit this form
  67. action_text - text of submit button
  68. class_ - sets a class for form
  69. #}
  70. {% macro render_form(form, action_url='', action_text='Submit', class_='', btn_class='btn btn-default') -%}
  71. <form method="POST" action="{{ action_url }}" role="form" class="{{ class_ }}" data-parsley-validate>
  72. {{ form.hidden_tag() if form.hidden_tag }}
  73. {% if caller %}
  74. {{ caller() }}
  75. {% else %}
  76. {% for f in form %}
  77. {% if f.type == 'BooleanField' %}
  78. {{ render_checkbox_field(f) }}
  79. {% elif f.type == 'RadioField' %}
  80. {{ render_radio_field(f) }}
  81. {% else %}
  82. {{ render_field(f) }}
  83. {% endif %}
  84. {% endfor %}
  85. {% endif %}
  86. <button type="submit" class="{{ btn_class }}">{{ action_text }} </button>
  87. </form>
  88. {%- endmacro %}