{# == Form Helpers == Macros to create form input, select, radio, checkbox, textarea compatible to BS3 ::Input: @value {{ f.tag('input', 'name', value='John', data={"k":"v", "k":"v"}) }} -> {{ f.tag('input', 'fileSelect', type='file') }} -> ::Hidden: @value {{ f.tag('hidden', 'name', value='John') }} -> ::Textarea: @value {{ f.tag('textarea', 'feedback', value='Irving') }} -> :: Button: @type, @name is the value of the form {{ f.tag('button', 'Save', type='submit')}} -> :: Checkbox: @options=[[k, v], [k, v]], @checked=[], @inline=bool {{ f.tag('checkbox', 'test', options=[[1,'Male'], [2,'Female']], checked=[1]) }}
:: Radio: @options=[[k, v], [k, v]], @checked=[], @inline=bool {{ f.tag('radio', 'test', options=[[1,'Male'], [2,'Female']], checked=[1]) }}
:: Select: @options=[[k, v], [k, v]], @selected, @multiple=bool, null_option=True {{ f.tag('select', 'gender', options=[['m', 'Male'], ['f', 'Female']], selected='f') }} :: Select-optgroup: @options=[[k, v], [k, v]], @selected {{ f.tag('select-optgroup', 'gender', options={"List of genders": [['m', 'Male'], ['f', 'Female']], ...}, selected='f') }} #} {% macro tag(_tag, name) -%} {% set _class = kwargs.pop('class', 'form-control') %} {% set _data = kwargs.pop('data', {}) %} {# input or hidden field #} {%- if _tag == "input" or _tag == "hidden" -%} {%- set _type = kwargs.pop('type', 'text') %} {# Hidden field #} {% if _tag == "hidden" %} {% set _type = "hidden" %} {% endif %} {# textarea: @value #} {% elif _tag == "textarea" -%} {% set value = kwargs.pop('value', '') %} {# button: @name=value #} {% elif _tag == "button" -%} {% set _type = kwargs.pop('type', 'button') %} {% set value = name %} {# radio|checkbox: @checked=[], @options=[[k, v], [k,v]], @inline=bool #} {% elif _tag in ['radio', 'checkbox'] -%} {% set checked = kwargs.pop('checked', []) %} {% set options = kwargs.pop('options', []) %} {% set display_inline = kwargs.pop('inline', False) %} {% for item in options %}
{% endfor %} {# select: @options=[[k, v], [k, v]], @selected=str, @multiple=bool #} {%- elif _tag == "select" -%} {% set selected = kwargs.pop('selected', '') %} {% set options = kwargs.pop('options', []) %} {% set multiple = kwargs.pop('multiple', False) %} {% set null_option = kwargs.pop('null_option', True) %} {# select-optgroup: @options={"group_name":[[k, v], [k, v]], ... }, @selected=str #} {%- elif _tag == "select-optgroup" -%} {% set selected = kwargs.pop('selected', '') %} {% set groups = kwargs.pop('options', {}) %} {%- endif -%} {%- endmacro %} {# -- Shortcuts for the above tags, with the option to wrap label around it #} {# To wrap with a label, just add the label attributes #} {% macro input(name) %} {% set _tag = "input" %} {% if "label" in kwargs %} {{ group_label(_tag, name, **kwargs) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro textarea(name) %} {% set _tag = "textarea" %} {% if "label" in kwargs %} {{ group_label(_tag, name, **kwargs) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro checkbox(name) %} {% set _tag = "checkbox" %} {% if "label" in kwargs %} {{ group_label(_tag, name, **kwargs) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro radio(name) %} {% set _tag = "radio" %} {% if "label" in kwargs %} {{ group_label(_tag, name, **kwargs) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro select(name) %} {% set _tag = "select" %} {% if "label" in kwargs %} {{ group_label(_tag, name, **kwargs) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro select_optgroup(name) %} {% set _tag = "select-optgroup" %} {% if "label" in kwargs %} {{ group_label(_tag, name) }} {% else %} {{ tag(_tag, name, **kwargs) }} {% endif %} {% endmacro %} {% macro hidden(name) %} {{ tag("hidden", name, **kwargs) }} {% endmacro %} {% macro button(name) %} {% set _class = "btn " + kwargs.pop("class", "btn") %}
{{ tag("button", name, class=_class, **kwargs) }}
{% endmacro %} {% macro recaptcha() %}
{{ recaptcha }}
{% endmacro %} {# ----- #} {#:: Label {{ f.label('name', 'First name') }} -> {{ f.label('name') }} -> {{ f.label('name', class='label', id='name-label') }} -> #} {% macro label(for_name, title) %} {% endmacro %} {# ----- #} {#:: group_label Wrap with form group and the label #} {% macro group_label(_tag, name) %} {% set _label = kwargs.pop('label', '') %}
{{ label(name, _label) }} {{ tag(_tag, name, **kwargs) }}
{% endmacro %} {% macro group(class="") %}
{{ caller() }}
{% endmacro %} {#:: CSRF_FIELD To secure post form by adding a csrf field in hit :#} {% macro csrf_field() %} {{ tag('hidden', '_csrf_token', value=csrf_token() ) }} {% endmacro %} {# == Form Creation == The macros below will wrap your form content in the
tag. These macros are callable macros, meaning you must use {% call %} {% endcall %} instead of your normal {% macro %} For POST and UPLOAD form, it will add the CSRF field in it. :: get : Create a simple GET form {% call get('Index:info', id='my_id') %} {{ tag('input', 'firstname', type='text') }} {% endcall %} ->
:: post : To create a POST form that will have the CSRF id for security {% call post('Index:info', id='my_id') %} {{ tag('input', 'firstname', type='text') }} {% endcall %} ->
:: upload : Create an Upload form, which will be a POST and have the CSRF id {% call upload('Index:info', id='my_id') %} {{ tag('input', 'firstname', type='text') }} {% endcall %} ->
#} {% macro __form(endpoint=None) %} {% set action = kwargs.pop("action", "") %} {% if endpoint %} {% set action = url_for(endpoint) %} {% endif %}
{{ caller() }}
{% endmacro %} {# POST #} {% macro post(endpoint=None) %} {% set _ = caller() %} {% call __form(endpoint, method="POST", **kwargs) %} {{ csrf_field() }} {{ _ }} {% endcall %} {% endmacro %} {# GET #} {% macro get(endpoint=None) %} {% set _ = caller() %} {% call __form(endpoint, method="GET", **kwargs) %} {{ _ }} {% endcall %} {% endmacro %} {# UPLOAD #} {% macro upload(endpoint=None) %} {% set _ = caller() %} {% call __form(endpoint, method="POST", enctype="multipart/form-data", **kwargs) %} {{ csrf_field() }} {{ _ }} {% endcall %} {% endmacro %}