FormEncode-Jinja2 is a Jinja2 extension for filling HTML forms via FormEncode.
You can install it from PyPI:
$ pip install FormEncode-Jinja2 # or
$ easy_install FormEncode-Jinja2
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>
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: |
|
---|---|
Returns: | rendered forms |
This extension provides the additional variables in the Jinja2 environment:
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.