FormEncode-Jinja2

FormEncode-Jinja2 is a Jinja2 extension for filling HTML forms via FormEncode.

Installation

You can install it from PyPI:

$ pip install FormEncode-Jinja2  # or
$ easy_install FormEncode-Jinja2

Basic Usage

Simple example in the interactive mode:

>>> import jinja2
>>> import formencode_jinja2
>>> env = jinja2.Environment(extensions=[formencode_jinja2.formfill])
>>> # or if there is already the Jinja environment:
>>> env.add_extension(formencode_jinja2.formfill)
>>> template = '''
... {%- formfill {'username': 'robert', 'email': 'robert153@usrobots.com'}
...        with {'username': 'This name is invalid'} -%}
...     <input type="text" name="username" />
...     <form:error name="username">
...     <input type="password" name="password" />
...     <input type="email" name="email" />
... </form>
... {%- endformfill -%}
... '''
>>> print env.from_string(template).render()
<input type="text" name="username" class="error" value="robert" />
    <span class="error-message">This name is invalid</span>
    <input type="password" name="password" value="" />
    <input type="email" name="email" value="robert153@usrobots.com" />
</form>

Flask

from flask import Flask
import formencode_jinja2

app = Flask(__name__)
app.jinja_env.add_extension(formencode_jinja2.formfill)

Reference

class formencode_jinja2.formfill.FormFillExtension(environment)[source]

Jinja2 extension for filling HTML forms via formencode.htmlfill.

For example, this code:

{% formfill {'username': 'robert', 'email': 'robert153@usrobots.com'}
       with {'username': 'This name is invalid'} %}
<form action="/register" method="POST">
    <input type="text" name="username" />
    <form:error name="username">
    <input type="password" name="password" />
    <input type="email" name="email" />
</form>
{% endformfill %}

will be rendered like below:

<form action="/register" method="POST">
    <input type="text" name="username" class="error" value="robert" />
    <span class="error-message">This name is invalid</span>
    <input type="password" name="password" value="" />
    <input type="email" name="email" value="robert153@usrobots.com" />
</form>

Syntax:

{% formfill <defaults> [with <errors>] %}
    body
{% endformfill %}
Parameters:
  • defaults – a mapping that contains default values of the input field (including select and textarea) surrounded in the template tag. Keys contain a value of name attribute of the input field, and values contain its default value.
  • errors – a mapping that contains error messages of the input fields. this value will also effect class attribute of the input field.
Returns:

rendered forms

This extension provides the additional variables in the Jinja2 environment:

jinja2.Environment.formfill_config

The default rendering configuration of the formfill tag. This property accepts the same arguments of formencode.htmlfill.render(), except form, defaults, errors and error_formatters.

jinja2.Environment.formfill_error_formatters

The mapping of error formatters and its name. Formatters are functions or callable objects that take the error text as a single argument, and returns a formatted text as a string.

Indices and tables

Table Of Contents

Next topic

Changelog

This Page