{# Form -- These macros are designed for building forms. They should be called like so: -------------------------------------------------------------------------------- {%- import "macros/form.html" as _form with context -%} {% call _form.form(form) %} {% call _form.fieldset('My legend 1') %} {{ _form.field(form.field1) }} {{ _form.field(form.field2) }} {% endcall %} {% call _form.fieldset('My legend 2') %} {% call _form.aside() %}
This text describes how the fields in this fieldset work.
{% endcall %} {% call _form.field() %} {{ _form.control(form.field3a) }} {{ _form.control(form.field3b) }} {% endcall %} {{ _form.field(form.field4) }} {% endcall %} {% call _form.buttons() %} {{ _form.button('Save') }} {{ _form.button('Cancel', url='/some-url') }} {% endcall %} {% endcall %} -------------------------------------------------------------------------------- #} {# Field types -- A list of the different type of fields that are accepted and the associated name they are given for formatting. There are currently 5 different form field styles - text, select, textarea, tick, ticks, and hidden. If a field type isn't listed below, it is assumed to be a `text` field. If you create another form type, it should be added to the list below and given an appropriate type. #} {% set field_types = { 'AssetField': 'asset', 'BooleanField': 'tick', 'CheckboxField': 'ticks', 'DateField': 'text', 'DateTimeField': 'text', 'DocumentCheckboxField': 'ticks', 'DocumentSelectField': 'select', 'FloatField': 'text', 'HiddenField': 'hidden', 'IntegerField': 'text', 'LatLonField': 'formfield', 'PasswordField': 'text', 'PriceField': 'text', 'RadioField': 'ticks', 'ReCAPTCHAField': 'captcha', 'SelectField': 'select', 'SlugField': 'text', 'StringField': 'text', 'StringListField': 'textarea', 'TextField': 'text', 'TimeField': 'text', 'TextAreaField': 'textarea', 'YesNoField': 'ticks', '_Option': 'tick' } %} {# Form -- The main containing form element. Unless an action is provided, the form macro will try to obtain the action from the current URL using `request.full_path` (if it exists). Form methods are assumed to be `POST` unless a `method` is provided. The macro will loop through `form` and insert all `hidden` field types (see field_types) into the top of the form and so these do not need to be included manually when the macro is called. -------------------------------------------------------------------------------- {% call _form.form(form) %} ... {% endcall %} -------------------------------------------------------------------------------- As standard you can also pass `class` to apply a custom class style to the containing element. You can also pass any other HTML attribute using keyword arguments and these will be applied to the container. -------------------------------------------------------------------------------- {% call _form.form( form=form, action='/some-url', method='POST', class='some-class', **{'data-some-attribute': 'Some value'} ) %} ... {% endcall %} -------------------------------------------------------------------------------- #} {% macro form(form, action=None, method='POST', class=None) -%} {% if action == None %} {%- set args = request.view_args or {} -%} {%- set _ = args.update(request.args or {}) -%} {%- set action = url_for(request.endpoint, **args) -%} {% endif %} {%- endmacro %} {# Fieldset -- A fieldset element containing multiple form fields. The fieldset macro is not required as part of the form construction, but can be used to improve the visual appearance of a form. It allows you to break up the form fields into different sections, optionally providing each section with a legend. -------------------------------------------------------------------------------- {% call _form.fieldset('My legend') %} ... {% endcall %} -------------------------------------------------------------------------------- As standard you can also pass `class` to apply a custom class style to the containing element. You can also pass any other HTML attribute using keyword arguments and these will be applied to the container. You can use this same approach to pass HTML attributes to the `legend` macros. -------------------------------------------------------------------------------- {% call _form.fieldset( legend_text='My legend', class='some-class', legend_attrs={'data-some-attribute': 'Some value'}, **{'data-some-attribute': 'Some value'} ) %} ... {% endcall %} -------------------------------------------------------------------------------- #} {% macro fieldset(legend_text=None, class=None, legend_attrs=None) -%} {%- set legend_attrs = legend_attrs or {} -%} {%- endmacro %} {# Legend -- A legend element inserted into the top of a `fieldset` macro. -------------------------------------------------------------------------------- {{ _form.legend('My legend') }} -------------------------------------------------------------------------------- As standard you can also pass `class` to apply a custom class style to the containing element. You can also pass any other HTML attribute using keyword arguments and these will be applied to the container. -------------------------------------------------------------------------------- {{ _form.legend( text='My legend', class='some-class', **{'data-some-attribute': 'Some value'} ) }} -------------------------------------------------------------------------------- #} {% macro legend(text, class=None) -%} {%- endmacro %} {# Aside -- A block of HTML used to describe the functionality of it's adjacent fields. In order to use this macro correctly it should be inserted at the top of the form or fieldset macro. -------------------------------------------------------------------------------- {% call _form.aside() %} ... {% endcall %} -------------------------------------------------------------------------------- As standard you can also pass `class` to apply a custom class style to the containing element. You can also pass any other HTML attribute using keyword arguments and these will be applied to the container. -------------------------------------------------------------------------------- {% call _form.aside( class='some-class', **{'data-some-attribute': 'Some value'} ) %} ... {% endcall %} -------------------------------------------------------------------------------- #} {% macro aside(class=None) -%}