Skip to content

Commit

Permalink
Merge pull request #143 from ulope/widget_templates
Browse files Browse the repository at this point in the history
A distinct template for each distinct widget.
  • Loading branch information
gregmuellegger committed Mar 18, 2015
2 parents e8d462d + 0608e85 commit 40e5a19
Show file tree
Hide file tree
Showing 22 changed files with 74 additions and 35 deletions.
15 changes: 9 additions & 6 deletions docs/customization.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ Customization
Override default templates
--------------------------

Widgets have a ``template_name`` attribute that point to the template to use
when rendering the form. Default templates are provided, for instance the
default template for a ``TextInput`` and other input-type widgets is
``floppyforms/input.html``. You can easily override this template in your
project-level ``TEMPLATE_DIRS``, assuming they take precedence over app-level
templates.
Widgets have a ``template_name`` attribute that points to the template that is
used when rendering the form. Default templates are provided for all
:doc:`built-in widgets </widgets-reference>`. In most cases the default
implementation of these templates have no specific behaviour and simply inherit
from ``floppyforms/input.html``. They are provided mainly to give an easy
way for a site-wide customization of how a specifig widget is rendered.

You can easily override these templates in your project-level
``TEMPLATE_DIRS``, assuming they take precedence over app-level templates.

Custom widgets with custom templates
------------------------------------
Expand Down
36 changes: 18 additions & 18 deletions docs/widgets-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ For each widgets, the default class attributes.

.. attribute:: TextInput.template_name

``'floppyforms/input.html'``
``'floppyforms/text.html'``

.. attribute:: TextInput.input_type

Expand All @@ -38,7 +38,7 @@ For each widgets, the default class attributes.

.. attribute:: PasswordInput.template_name

``'floppyforms/input.html'``
``'floppyforms/password.html'``

.. attribute:: PasswordInput.input_type

Expand All @@ -48,7 +48,7 @@ For each widgets, the default class attributes.

.. attribute:: HiddenInput.template_name

``'floppyforms/input.html'``
``'floppyforms/hidden.html'``

.. attribute:: HiddenInput.input_type

Expand All @@ -58,7 +58,7 @@ For each widgets, the default class attributes.

.. attribute:: SlugInput.template_name

``'floppyforms/input.html'``
``'floppyforms/slug.html'``

.. attribute:: SlugInput.input_type

Expand All @@ -71,7 +71,7 @@ For each widgets, the default class attributes.

.. attribute:: IPAddressInput.template_name

``'floppyforms/input.html'``
``'floppyforms/ipaddress.html'``

.. attribute:: IPAddressInput.input_type

Expand All @@ -85,7 +85,7 @@ For each widgets, the default class attributes.

.. attribute:: FileInput.template_name

``'floppyforms/input.html'``
``'floppyforms/file.html'``

.. attribute:: FileInput.input_type

Expand All @@ -95,7 +95,7 @@ For each widgets, the default class attributes.

.. attribute:: ClearableFileInput.template_name

``'floppyforms/input.html'``
``'floppyforms/clearable_input.html'``

.. attribute:: ClearableFileInput.input_type

Expand All @@ -120,7 +120,7 @@ For each widgets, the default class attributes.

.. attribute:: EmailInput.template_name

``'floppyforms/input.html'``
``'floppyforms/email.html'``

.. attribute:: EmailInput.input_type

Expand All @@ -130,7 +130,7 @@ For each widgets, the default class attributes.

.. attribute:: URLInput.template_name

``'floppyforms/input.html'``
``'floppyforms/url.html'``

.. attribute:: URLInput.input_type

Expand All @@ -140,7 +140,7 @@ For each widgets, the default class attributes.

.. attribute:: SearchInput.template_name

``'floppyforms/input.html'``
``'floppyforms/search.html'``

.. attribute:: SearchInput.input_type

Expand All @@ -150,7 +150,7 @@ For each widgets, the default class attributes.

.. attribute:: ColorInput.template_name

``'floppyforms/input.html'``
``'floppyforms/color.html'``

.. attribute:: ColorInput.input_type

Expand All @@ -160,7 +160,7 @@ For each widgets, the default class attributes.

.. attribute:: PhoneNumberInput.template_name

``'floppyforms/input.html'``
``'floppyforms/phonenumber.html'``

.. attribute:: PhoneNumberInput.input_type

Expand All @@ -170,7 +170,7 @@ For each widgets, the default class attributes.

.. attribute:: DateInput.template_name

``'floppyforms/input.html'``
``'floppyforms/date.html'``

.. attribute:: DateInput.input_type

Expand All @@ -185,7 +185,7 @@ For each widgets, the default class attributes.

.. attribute:: DateTimeInput.template_name

``'floppyforms/input.html'``
``'floppyforms/datetime.html'``

.. attribute:: DateTimeInput.input_type

Expand All @@ -195,7 +195,7 @@ For each widgets, the default class attributes.

.. attribute:: TimeInput.template_name

``'floppyforms/input.html'``
``'floppyforms/time.html'``

.. attribute:: TimeInput.input_type

Expand All @@ -205,7 +205,7 @@ For each widgets, the default class attributes.

.. attribute:: NumberInput.template_name

``'floppyforms/input.html'``
``'floppyforms/number.html'``

.. attribute:: NumberInput.input_type

Expand All @@ -230,7 +230,7 @@ For each widgets, the default class attributes.

.. attribute:: NumberInput.template_name

``'floppyforms/input.html'``
``'floppyforms/range.html'``

.. attribute:: RangeInput.input_type

Expand Down Expand Up @@ -271,7 +271,7 @@ For each widgets, the default class attributes.

.. attribute:: CheckboxInput.template_name

``'floppyforms/input.html'``
``'floppyforms/checkbox.html'``

.. attribute:: CheckboxInput.input_type

Expand Down
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/checkbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/color.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/date.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/datetime.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/hidden.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/ipaddress.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/number.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/password.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/phonenumber.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/range.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/search.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/slug.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/text.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/time.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
1 change: 1 addition & 0 deletions floppyforms/templates/floppyforms/url.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% extends "floppyforms/input.html" %}
19 changes: 19 additions & 0 deletions floppyforms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def render(self, name, value, attrs=None, **kwargs):


class TextInput(Input):
template_name = 'floppyforms/text.html'
input_type = 'text'

def __init__(self, *args, **kwargs):
Expand All @@ -123,6 +124,7 @@ def __init__(self, *args, **kwargs):


class PasswordInput(TextInput):
template_name = 'floppyforms/password.html'
input_type = 'password'

def __init__(self, attrs=None, render_value=False):
Expand All @@ -136,6 +138,7 @@ def render(self, name, value, attrs=None):


class HiddenInput(Input):
template_name = 'floppyforms/hidden.html'
input_type = 'hidden'


Expand Down Expand Up @@ -168,6 +171,8 @@ def value_from_datadict(self, data, files, name):


class SlugInput(TextInput):
template_name = 'floppyforms/slug.html'

"""<input type="text"> validating slugs with a pattern"""
def get_context(self, name, value, attrs):
context = super(SlugInput, self).get_context(name, value, attrs)
Expand All @@ -176,6 +181,8 @@ def get_context(self, name, value, attrs):


class IPAddressInput(TextInput):
template_name = 'floppyforms/ipaddress.html'

"""<input type="text"> validating IP addresses with a pattern"""
ip_pattern = ("(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25"
"[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}")
Expand All @@ -187,6 +194,7 @@ def get_context(self, name, value, attrs):


class FileInput(Input):
template_name = 'floppyforms/file.html'
input_type = 'file'
needs_multipart_form = True
omit_value = True
Expand Down Expand Up @@ -266,6 +274,7 @@ def _format_value(self, value):


class DateInput(Input):
template_name = 'floppyforms/date.html'
input_type = 'date'
supports_microseconds = False

Expand All @@ -292,6 +301,7 @@ def _has_changed(self, initial, data):


class DateTimeInput(Input):
template_name = 'floppyforms/datetime.html'
input_type = 'datetime'
supports_microseconds = False

Expand Down Expand Up @@ -323,6 +333,7 @@ def _has_changed(self, initial, data):


class TimeInput(Input):
template_name = 'floppyforms/time.html'
input_type = 'time'
supports_microseconds = False

Expand Down Expand Up @@ -353,22 +364,27 @@ def _has_changed(self, initial, data):


class SearchInput(Input):
template_name = 'floppyforms/search.html'
input_type = 'search'


class EmailInput(TextInput):
template_name = 'floppyforms/email.html'
input_type = 'email'


class URLInput(TextInput):
template_name = 'floppyforms/url.html'
input_type = 'url'


class ColorInput(Input):
template_name = 'floppyforms/color.html'
input_type = 'color'


class NumberInput(TextInput):
template_name = 'floppyforms/number.html'
input_type = 'number'
min = None
max = None
Expand All @@ -386,10 +402,12 @@ def __init__(self, attrs=None):


class RangeInput(NumberInput):
template_name = 'floppyforms/range.html'
input_type = 'range'


class PhoneNumberInput(Input):
template_name = 'floppyforms/phonenumber.html'
input_type = 'tel'


Expand All @@ -398,6 +416,7 @@ def boolean_check(v):


class CheckboxInput(Input, forms.CheckboxInput):
template_name = 'floppyforms/checkbox.html'
input_type = 'checkbox'

def __init__(self, attrs=None, check_test=None):
Expand Down
2 changes: 1 addition & 1 deletion tests/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_default_retrieve(self):
# retrieve widget template

template_name = config.retrieve('widget_template', bound_field=form['name'])
self.assertEqual(template_name, 'floppyforms/input.html')
self.assertEqual(template_name, 'floppyforms/text.html')

template_name = config.retrieve('widget_template', bound_field=form['comment'])
self.assertEqual(template_name, 'floppyforms/textarea.html')
Expand Down

0 comments on commit 40e5a19

Please sign in to comment.