Formulation


or:

Oops, you got some presentation in your logic.

Where are we now?


ALL or Nothing:
  • Presentation in code (widgets)
  • Clumsy templates

The ideal


Business logic in code.

Presentation in templates.

Some help for designers from coders.

Existing solutions


django-floppyforms

THE PROBLEM:

Slow!

One template per field, 
PLUS one per row, 
PLUS one per form.

My Solution


Load one template.
Use {% block %} to define template fragments.

Born out of django-sniplates:

{% sniplate "template" "blockname" .... %}

A Sample

Simple as it gets:
{% form "formulation/default.form" %}
<form method="POST" action=".">
{% for field in form %}
{% field field %}
{% endfor %}
</form>
{% endform %} 

But how do I control my widgets?
{% form "formulation/default.form" %}
{% field myform.foo "somewidget" ... %}{% field myform.bar "otherwidget" ... %}{% endform %}

Which widget?

  • '{field}_{widget}_{name}'
  • '{field}_{name}'
  • '{widget}_{name}'
  • '{field}_{widget}'
  • '{name}'
  • '{widget}'
  • '{field}'
OR specify it manually in the field tag:
{% field form.myfield "widgetname" ... %} 

What's with the ... ?


Just like {% include %} you can pass extra context, or override what's provided.

Want to change the help text?

{% field field help_text="sod off!" %} 

Want to always have a blank value?

{% field field value="" %} 

Let's DRY this out a bit


{% use "widget" ... %}
Works like {% include %} but only for widgets in the current {% form %}

{% load reuse %}
{% reuse "blockname" ... %}

Works like {% include %} but for blocks within the current template. 

That's right folks: TEMPLATE MACROS!

Where to now?


{% render_form form widget_set %}

Renders a whole form in one go, relying on auto-widget.

Uses special block named "form" to define form rendering.

Find out more:



Formulation

By Curtis Maloney

Formulation

  • 1,374