Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#2375] Move autoform macro into it's own file
This allows us to dynamically pick the form macro to use and cleans up the template nicely.
- Loading branch information
Showing
2 changed files
with
66 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{# | ||
Builds a form from the supplied form_info list/tuple. All form info dicts | ||
can also contain an "extra_info" key which will add some help text after the | ||
input element. | ||
|
||
form_info - A list of dicts describing the form field to build. | ||
data - The form data object. | ||
errors - The form errors object. | ||
|
||
Example | ||
|
||
{% set form_info = [ | ||
{'name': 'ckan.site_title', 'control': 'input', 'label': _('Site Title'), 'placeholder': _('')}, | ||
{'name': 'ckan.main_css', 'control': 'select', 'options': styles, 'label': _('Style'), 'placeholder': _('')}, | ||
{'name': 'ckan.site_description', 'control': 'input', 'label': _('Site Tag Line'), 'placeholder': _('')}, | ||
{'name': 'ckan.site_logo', 'control': 'input', 'label': _('Site Tag Logo'), 'placeholder': _('')}, | ||
{'name': 'ckan.site_about', 'control': 'markdown', 'label': _('About'), 'placeholder': _('About page text')}, | ||
{'name': 'ckan.site_intro_text', 'control': 'markdown', 'label': _('Intro Text'), 'placeholder': _('Text on home page')}, | ||
{'name': 'ckan.site_custom_css', 'control': 'textarea', 'label': _('Custom CSS'), 'placeholder': _('Customisable css inserted into the page header')}, | ||
] %} | ||
|
||
{% import 'macros/autoform.html' as autoform %} | ||
{{ autoform.generate(form_info, data, errors) }} | ||
|
||
#} | ||
{% import 'macros/form.html' as form %} | ||
{%- macro generate(form_info=[], data={}, errors={}) -%} | ||
{% for item in form_info %} | ||
{% set name = item.name %} | ||
{% set value = data.get(name) %} | ||
{% set error = errors.get(name) %} | ||
{% set id = 'field-%s' % (name|lower|replace('_', '-')|replace('.', '-')) %} | ||
|
||
{% set control = item.control or 'input' %} | ||
{% set label = item.label %} | ||
{% set placeholder = item.placeholder %} | ||
|
||
{% set classes = item.classes or [] %} | ||
{% set classes = ['control-medium'] if not classes and control == 'input' %} | ||
|
||
{% if control == 'select' %} | ||
{% call form.select(name, id=id, label=label, options=item.options, selected=value, error=error) %} | ||
{% if item.extra_info %}{{ form.info(item.extra_info) }}{% endif %} | ||
{% endcall %} | ||
{% else %} | ||
{% call form[control](name, id=id, label=label, placeholder=placeholder, value=value, error=error, classes=classes) %} | ||
{% if item.extra_info %}{{ form.info(item.extra_info) }}{% endif %} | ||
{% endcall %} | ||
{% endif %} | ||
{% endfor %} | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters