diff --git a/docs/customization.rst b/docs/customization.rst index cfa2edd..f9591c5 100644 --- a/docs/customization.rst +++ b/docs/customization.rst @@ -108,3 +108,24 @@ If you want only the attribute's key to be rendered, set it to ``True``: return ctx This will simply add ``awesome`` as a key-only attribute. + +You can also pass an ``attrs`` dictionary to the ``{% formrow %}`` template tag, +which will then subsequently be passed to the rendered widget. + +Pass this to your context + +.. code-block:: python + + myfield_attrs = dict(placeholder="Some text") + +And use like this in your HTML + +.. code-block:: jinja + + {% formrow form.myfield with attrs=myfield_attrs %} + +Which will output something like + +.. code-block:: jinja + + diff --git a/floppyforms/templates/floppyforms/rows/li.html b/floppyforms/templates/floppyforms/rows/li.html index 42ce4b3..6327c00 100644 --- a/floppyforms/templates/floppyforms/rows/li.html +++ b/floppyforms/templates/floppyforms/rows/li.html @@ -3,7 +3,7 @@ {% block field %} {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} {% block label %}{% if field|id %}{% endif %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %} {% endblock %} diff --git a/floppyforms/templates/floppyforms/rows/p.html b/floppyforms/templates/floppyforms/rows/p.html index 62f2dfe..20bade3 100644 --- a/floppyforms/templates/floppyforms/rows/p.html +++ b/floppyforms/templates/floppyforms/rows/p.html @@ -4,7 +4,7 @@ {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} {% block label %}{% if field|id %}{% endif %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %}

{% endblock %} diff --git a/floppyforms/templates/floppyforms/rows/tr.html b/floppyforms/templates/floppyforms/rows/tr.html index dd01db6..8f8b82a 100644 --- a/floppyforms/templates/floppyforms/rows/tr.html +++ b/floppyforms/templates/floppyforms/rows/tr.html @@ -4,7 +4,7 @@ {% block label %}{% if field|id %}{% endif %}{% endblock %} {% block errors %}{% include "floppyforms/errors.html" with errors=field.errors %}{% endblock %} - {% block widget %}{% formfield field %}{% endblock %} + {% block widget %}{% formfield field with attrs=attrs %}{% endblock %} {% block help_text %}{% if help_text %}
{{ help_text }}{% endif %}{% endblock %} {% block hidden_fields %}{% for field in hidden_fields %}{{ field.as_hidden }}{% endfor %}{% endblock %} diff --git a/floppyforms/templatetags/floppyforms.py b/floppyforms/templatetags/floppyforms.py index b19ff71..1f444d9 100644 --- a/floppyforms/templatetags/floppyforms.py +++ b/floppyforms/templatetags/floppyforms.py @@ -623,6 +623,7 @@ def render(self, context): extra_context = self.get_extra_context(context) template_name = config.retrieve('widget_template', bound_field=bound_field) + attrs = extra_context.pop('attrs', {}) if 'using' in self.options: try: template_name = self.options['using'].resolve(context) @@ -643,7 +644,7 @@ def render(self, context): # template name and context instance parameters with attributes(widget, template_name=template_name, context_instance=context_instance) as widget: - output = bound_field.as_widget(widget=widget) + output = bound_field.as_widget(widget=widget, attrs=attrs) config.pop() diff --git a/tests/templatetags.py b/tests/templatetags.py index 59cd2c3..98c0c57 100644 --- a/tests/templatetags.py +++ b/tests/templatetags.py @@ -668,6 +668,22 @@ def test_formconfig_gets_popped_after_formrow_tag(self): Extra argument: first argument ''') + def test_attrs_are_passed_to_widget(self): + + form = SimpleForm() + name_attrs = {'placeholder': 'My Name'} + rendered = render('''{% form form using %} + {% formrow form.name with attrs=name_attrs %} + {% endform %}''', {'form': form, 'name_attrs': name_attrs}) + + self.assertHTMLEqual(rendered, ''' +

+ + +

+ ''') + + class FormFieldTagTests(TestCase): def test_valid_syntax(self):