Pylons

Jun 30, 2009 7:39:21 PM

Using htmlfill to fill forms in

Tags: mako pylons
This snippet shows how to use a Mako def as a Mako filter to fill in a form.

First, in a Mako library file (ie, some arbitrary Mako template that you store a lot of Mako def's you use elsewhere), add a def like so:

<%def name="formfill(content)">
<% errors = c.form_errors or {} %>
% if not errors and not request.params:
${content |n}
% else:
${htmlfill.render(content, request.params.mixed(), errors) | n}
% endif
</%def>

## This is needed to import htmlfill
<%!
from formencode import htmlfill
%>

Then elsewhere, wherever you have a form, wrap it in a def like so:

${search_form()}

<%def name="search_form()" filter="widget.formfill">
    <form action.....>
    .....
    </form>
</%def>
<%namespace name="widget" file="/widgets.mako"/>

This is assuming the formfill def was in a /widgets.mako file, which is where I usually keep various def's I used all over.

Also note that I pull errors from c.form_errors, and if there's no form_errors populated, and no request params the form is rendered without being filled with htmlfill. Feel free to change as needed.

Comments (66)

Ergo
Dec 30, 2009 6:25:13 PM

Thats a really interesting idea :-) thanks

You must login before you can comment.

Powered by Pylons - Contact Administrators