Widgets

reform.widget.checkbox(attribute, *args, **kwargs)

Return an input of type checkbox. It uses attribute.value to determine if the checkbox is checked. You can override this by passing the parameter checked with a boolean:

checkbox(..., checked=True) or checkbox(..., checked=False)

Note that the returned tag wont have any value attribute, since it is irrelevant in checkboxes.

reform.widget.xml_attrs(*args, **kwargs)

Convert the mapping kwargs into XML style attributes. It returns a Markup object thats convertible to an unicode string or a regular str string:

>>> s = xml_attrs(attribute='my attribute')
>>> s
Markup(u"attribute='my attribute'")
>>> unicode(s)
u"attribute='my attribute'"
>>> str(s)
"attribute='my attribute'"

There’s no need to escape specials characters before calling xml_attrs:

>>> xml_attrs(attribute="'';!--\"<XSS>=&{()}")
Markup(u"attribute='&#39;&#39;;!--&#34;&lt;XSS&gt;=&amp;{()}'")

If you need to have attributes in order, you can pass a list of 2-tuples:

>>> xml_attrs([('one', 1), ('two', 'II'), ('three', 'thr33')])
Markup(u"one='1' two='II' three='thr33'")
You can mix both methods, the keyword arguments will be at the end:
>>> xml_attrs([('first', 1)], second=2)
Markup(u"first='1' second='2'")

If a value is True, its name is used as value:

>>> xml_attrs(checked=True)
Markup(u"checked='checked'")

If a value is None or False, the attribute is ignored:

>>> xml_attrs(foo=None, bar=False)
Markup(u'')

If an attribute is specified twice, only the last value will be kept:

>>> xml_attrs([('foo', 'ignored'), ('foo', 'kept')])
Markup(u"foo='kept'")
>>> xml_attrs([('foo', 'ignored'), ('foo', 'ignored too')],
...           foo='kept')
Markup(u"foo='kept'")
reform.widget.input(attribute, *args, **kwargs)

Return a generic input tag:

>>> input(Attribute(name='my name', data='my data'))
Markup(u"<input id='my name' name='my name' value='my data'/>")

By default input add an attribute id to the tag. If you want to override it pass the keyword argument id:

>>> input(Attribute(name='my name', data='my data'), id=None)
Markup(u"<input name='my name' value='my data'/>")
>>> input(Attribute(name='my name', data='my data'), id='my id')
Markup(u"<input id='my id' name='my name' value='my data'/>")

Use keywords arguments to add more attributes to the input:

>>> input(Attribute(name='my name', data='my data'), type='hidden')
Markup(u"<input id='my name' name='my name' value='my data' type='hidden'/>")

class is a python keyword, which mean you can’t use it as a keyword argument. To specify the class of an input, use class_:

>>> input(Attribute(name='my name', data='my data'), class_='x')
Markup(u"<input id='my name' name='my name' value='my data' class='x'/>")

Previous topic

Fields

This Page