Skip to content

Commit

Permalink
implemented button_map for render_form_row (#44)
Browse files Browse the repository at this point in the history
* implemented button_map for render_form_row

* added button_map documentation for render_form_row
  • Loading branch information
PanderMusubi authored Apr 8, 2020
1 parent 308475b commit 3f58b72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 6 additions & 1 deletion docs/macros.rst
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ API
.. py:function:: render_form_row(fields,\
row_class='form-row',\
col_class_default='col',\
col_map={})
col_map={},
button_map={})
Render a bootstrap row with the given fields

Expand All @@ -203,6 +204,10 @@ API
if nothing more specific is said for the div column of the rendered field.
:param col_map: A dictionary, mapping field.name to a class definition that should be applied to
the div column that contains the field. For example: ``col_map={'username': 'col-md-2'})``
:param button_map: A dictionary, mapping button field names to Bootstrap category names such as
``primary``, ``danger`` or ``success``. Buttons not found
in the ``button_map`` will use the ``secondary`` type of
button.



Expand Down
12 changes: 8 additions & 4 deletions flask_bootstrap/templates/bootstrap/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@
</form>
{%- endmacro %}

{% macro render_form_row(fields, row_class='form-row', col_class_default='col', col_map={}) %}
{% macro render_form_row(fields, row_class='form-row', col_class_default='col', col_map={}, button_map={}) %}
{#
fields: an iterable of fields to render in a row.
row_class: Class to apply to the div intended to represent the row, like
'form-row' or 'row'.
col_class_default: Default col class to apply if nothing more specific is said about a field.
col_class_dict: A dictionary which keys are fields.name and which values are the col class to apply to the field.
col_map: A dictionary which keys are fields.name and which values are the col class to apply to the field.
button_map: A dictionary, mapping button field names to Bootstrap category names.

Usages:
Easiest use with defaults:
Expand All @@ -261,8 +262,11 @@
Custom col which should use class col-md-2, and the others the defaults:
{{ render_form_row([form.title, form.first_name, form.surname], col_map={'title': 'col-md-2'}) }}

Custom col which should use class col-md-2 and modified col class for the default of the other fields
Custom col which should use class col-md-2 and modified col class for the default of the other fields:
{{ render_form_row([form.title, form.first_name, form.surname], col_class_default='col-md-5', col_map={'title': 'col-md-2'}) }}

Mapping Bootstrap button category to field name:
{{ render_form_row([form.cancel, form.submit], button_map={'submit': 'primary'}) }}
#}
<div class="{{ row_class }}">
{% for field in fields %}
Expand All @@ -272,7 +276,7 @@
{% set col_class = col_class_default %}
{% endif %}
<div class="{{ col_class }}">
{{ render_field(field) }}
{{ render_field(field, button_map=button_map) }}
</div>
{% endfor %}
</div>
Expand Down

0 comments on commit 3f58b72

Please sign in to comment.