.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the form-group.
is_required - Boolean of whether this input is requred for the form to validate
Examples:
{% import 'macros/form.html' as form %}
{{ form.select('year', label=_('Year'), options=[{'value':2010, 'text': 2010},{'value': 2011, 'text': 2011}], selected=2011, error=errors.year) }}
Or only with values if they are the same as text:
{{ form.select('year', label=_('Year'), options=[{'value':2010},{'value': 2011}], selected=2011, error=errors.year) }}
Complete example:
{{ form.select('the_data_type', id='the_data_type', label=_('Data Type'), options=[
{'value': '0', 'text': _('[Choose Data Type]')}
, {'value': '1', 'text': _('Private data')}
, {'value': '2', 'text': _('Not private data')}
], selected=data.the_data_type if data.the_data_type else '0', is_required=true) }}
#}
{% macro select(name, id='', label='', options='', selected='', error='', classes=[], attrs={'class': 'form-control'}, is_required=false) %}
{% set classes = (classes|list) %}
{% do classes.append('control-select') %}
{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
{% for option in options %}
{{ option.text or option.value }}
{% endfor %}
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for a Markdown textarea element. Handles
matching labels to inputs, selected item and error messages.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input.
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the form-group.
is_required - Boolean of whether this input is requred for the form to validate
Examples:
{% import 'macros/form.html' as form %}
{{ form.markdown('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc) }}
#}
{% macro markdown(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={'class': 'form-control'}, is_required=false) %}
{% set classes = (classes|list) %}
{% do classes.append('control-full') %}
{% set markdown_tooltip = "__Bold text__ or _italic text_
# title ## secondary title ### etc
* list * of * items
http://auto.link.ed/
Full markdown syntax
Please note: HTML tags are stripped out for security reasons
" %}
{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, control_classes=["editor"], extra_html=extra_html, is_required=is_required) %}
{% trans %}You can use Markdown formatting here{% endtrans %}
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for a plain textarea element. Handles
matching labels to inputs, selected item and error messages.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
value - The value of the input.
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the form-group.
is_required - Boolean of whether this input is requred for the form to validate
Examples:
{% import 'macros/form.html' as form %}
{{ form.textarea('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc) }}
#}
{% macro textarea(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={'class': 'form-control'}, is_required=false, rows=5, cols=20) %}
{% set classes = (classes|list) %}
{% do classes.append('control-full') %}
{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for an input element with a prefixed segment.
These are useful for showing url slugs and other fields where the input
information forms only part of the saved data.
name - The name of the form parameter.
id - The id to use on the input and label. Convention is to prefix with 'field-'.
label - The human readable label.
prepend - The text that will be prepended before the input.
value - The value of the input.
which will use the name key as the value.
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the form-group.
is_required - Boolean of whether this input is requred for the form to validate
Examples:
{% import 'macros/form.html' as form %}
{{ form.prepend('slug', id='field-slug', prepend='/dataset/', label=_('Slug'), value=data.slug, error=errors.slug) }}
#}
{% macro prepend(name, id='', label='', prepend='', value='', placeholder='', type='text', error="", classes=[], attrs={'class': 'form-control'}, is_required=false) %}
{# We manually append the error here as it needs to be inside the .input-group block #}
{% set classes = (classes|list) %}
{% do classes.append('error') if error %}
{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error='', classes=classes, extra_html=extra_html, is_required=is_required) %}
{% if prepend %}{{ prepend }} {%- endif -%}
{% if error and error is iterable %}{{ error|join(', ') }} {% endif %}
{% endcall %}
{% endmacro %}
{#
Creates all the markup required for an custom key/value input. These are usually
used to let the user provide custom meta data. Each "field" has three inputs
one for the key, one for the value and a checkbox to remove it. So the arguments
for this macro are nearly all tuples containing values for the
(key, value, delete) fields respectively.
name - A tuple of names for the three fields.
id - An id string to be used for each input.
label - The human readable label for the main label.
values - A tuple of values for the (key, value, delete) fields. If delete
is truthy the checkbox will be checked.
placeholder - A tuple of placeholder text for the (key, value) fields.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the form-group.
is_required - Boolean of whether this input is requred for the form to validate
Examples:
{% import 'macros/form.html' as form %}
{{ form.custom(
names=('custom_key', 'custom_value', 'custom_deleted'),
id='field-custom',
label=_('Custom Field'),
values=(extra.key, extra.value, extra.deleted),
error=''
) }}
#}
{% macro custom(names=(), id="", label="", values=(), placeholders=(), error="", classes=[], attrs={}, is_required=false, key_values=()) %}
{%- set classes = (classes|list) -%}
{%- set label_id = (id or names[0]) ~ "-key" -%}
{%- set extra_html = caller() if caller -%}
{%- do classes.append('control-custom') -%}
{% call input_block(label_id, label or name, error, classes, control_classes=["editor"], extra_html=extra_html, is_required=is_required) %}
{% endcall %}
{% endmacro %}
{#
A generic input_block for providing the default markup for CKAN form elements.
It is expected to be called using a {% call %} block, the contents of which
will be inserted into the .controls element.
for - The id for the input that the label should match.
label - A human readable label.
error - A list of error strings for the field or just true.
classes - An array of custom classes for the outer element.
control_classes - An array of custom classes for the .control wrapper.
extra_html - An html string to be inserted after the errors eg. info text.
is_required - Boolean of whether this input is requred for the form to validate
Example:
{% import 'macros/form.html' as form %}
{% call form.input_block("field", "My Field") %}
{% endcall %}
#}
{% macro input_block(for, label="", error="", classes=[], control_classes=[], extra_html="", is_required=false) %}
{% endmacro %}
{#
Builds a list of errors for the current form.
errors - A dict of field/message pairs.
type - The alert-* class that should be applied (default: "error")
classes - A list of classes to apply to the wrapper (default: [])
Example:
{% import 'macros/form.html' as form %}
{{ form.errors(error_summary, type="warning") }}
#}
{% macro errors(errors={}, type="error", classes=[]) %}
{% if errors %}
{{ _('The form contains invalid entries:') }}
{% for key, error in errors.items() %}
{% if key %}{{ key }}: {% endif %}{{ error }}
{% endfor %}
{% endif %}
{% endmacro %}
{#
Renders an info box with a description. This will usually be used with in a
call block when creating an input element.
text - The text to include in the box.
inline - If true displays the info box inline with the input.
classes - A list of classes to add to the info box.
Example
{% import 'macros/form.html' as form %}
{% call form.input('name') %}
{{ form.info(_('My useful help text')) }}
{% endcall %}
#}
{% macro info(text='', inline=false, classes=[]) %}
{%- if text -%}
{{ text }}
{%- endif -%}
{% endmacro %}
{#
Builds a single hidden input.
name - name of the hidden input
value - value of the hidden input
Example
{% import 'macros/form.html' as form %}
{{ form.hidden('name', 'value') }}
#}
{% macro hidden(name, value) %}
{% endmacro %}
{#
Contructs hidden inputs for each name-value pair.
fields - [('name1', 'value1'), ('name2', 'value2'), ...]
Two parameter for excluding several names or name-value pairs.
except_names - list of names to be excluded
except - list of name-value pairs to be excluded
Example:
{% import 'macros/form.html' as form %}
{% form.hidden_from_list(fields=c.fields, except=[('topic', 'xyz')]) %}
{% form.hidden_from_list(fields=c.fields, except_names=['time_min', 'time_max']) %}
#}
{% macro hidden_from_list(fields, except_names=None, except=None) %}
{% set except_names = except_names or [] %}
{% set except = except or [] %}
{% for name, value in fields %}
{% if name and value and name not in except_names and (name, value) not in except %}
{{ hidden(name, value) }}
{% endif %}
{% endfor %}
{% endmacro %}
{#
Builds a space seperated list of html attributes from a dict of key/value pairs.
Generally only used internally by macros.
attrs - A dict of attribute/value pairs
Example
{% import 'macros/form.html' as form %}
{{ form.attributes({}) }}
#}
{%- macro attributes(attrs={}) -%}
{%- for key, value in attrs.items() -%}
{{ " " }}{{ key }}{% if value != "" %}="{{ value }}"{% endif %}
{%- endfor -%}
{%- endmacro -%}
{#
Outputs the "* Required field" message for the bottom of formss
Example
{% import 'macros/form.html' as form %}
{{ form.required_message() }}
#}
{% macro required_message() %}
* {{ _("Required field") }}
{% endmacro %}
{#
Builds a file upload for input
Example
{% import 'macros/form.html' as form %}
{{ form.image_upload(data, errors, is_upload_enabled=true) }}
#}
{% macro image_upload(data, errors, field_url='image_url', field_upload='image_upload', field_clear='clear_upload',
is_url=false, is_upload=false, is_upload_enabled=false, placeholder=false,
url_label='', upload_label='', field_name='image_url') %}
{% set placeholder = placeholder if placeholder else _('http://example.com/my-image.jpg') %}
{% set url_label = url_label or _('Image URL') %}
{% set upload_label = upload_label or _('Image') %}
{% set previous_upload = data['previous_upload'] %}
{% if is_upload_enabled %}
{% set upload_name = upload_label ~ '-' ~ field_upload %}
{% endif %}
{{ input(field_url, label=url_label, id='field-image-url', type='url', placeholder=placeholder, value=data.get(field_url), error=errors.get(field_url), classes=['control-full']) }}
{% if is_upload_enabled %}
{{ input(upload_name, label=upload_label, id=upload_label, type='file', placeholder='', value='', error='', classes=['control-full', 'resource-upload']) }}
{% if is_upload %}
{{ checkbox(field_clear, label=_('Clear Upload'), id='field-clear-upload', value='true', error='', classes=['control-full']) }}
{% endif %}
{% endif %}
{% if is_upload_enabled %}
{% endif %}
{% endmacro %}