Quantcast
Channel: Yann's Blog » Python
Viewing all articles
Browse latest Browse all 10

yyafl now has layout support, with decorators

$
0
0

Just finished adding in a flexible layout system to yyafl, my reimplementation of Django newforms for other Python web frameworks or WSGI adapters. Now its simple to add default layouts or render a different layout on demand.

For example, to add a simple TableLayout() to a form:

class Form1(yyafl.Form):
    name = fields.CharField(label = "User name", required = True)
    email = fields.CharField(label = "Your e-mail address", required = True)
    hidden = fields.CharField(widget = HiddenInput, default = "123")
    _layout = yyafl.layout.TableLayout()

and to render the form to HTML:

# Render using the layout specified in _layout above.
content += f.render()
# Or invoke the layout explicitly
l = yyafl.layout.TableLayout()
content += l.render(f)

yyafl Layout()s also understand decorators:

decorators = { '*' : MarkRequiredDecorator(),
    'name_field' : HighlightDecorator() }
layout = yyafl.layout.TableLayout(decorators = decorators)
layout.render(f)

Decorators can change attributes and wrap fields in markup blocks. In the above example, the ‘*’ signifies the decorator should apply to all fields, and the HighlightDecorator should only apply to the field called ‘name_field’.

There are still some not yet implemented ideas:

  • It doesn’t generated the <form> blocks yet, you need to roll your own.
  • Field reordering and grouping isn’t supported by the default layout engines yet, though you could write your own.
  • No AJAX form support (though its coming!)
  • Automatic CSRF prevention forms.
  • Error message printing in the form.

You can find these latest changes in the Git repository linked from the main yyafl web page.


Viewing all articles
Browse latest Browse all 10

Trending Articles