Contents of this module are mostly meant to be used inside the view templates, but their usage is not limited to templates by any means. bottle_utils.html
contains a few data-formatting functions as well as shortcuts for generating HTML snippets and binding data to form fields.
One way to make the module contents available to templates is to add the module itself as a default template variable.:
import bottle
from bottle_utils import html
bottle.BaseTemplate.defaults['h'] = html
This allows you to use the module members by access the h
variable in templates:
<html {{! h.attr('lang', request.locale }}>
Note
If your tempate engine auto-escapes HTML, you need to instruct it to unescape strings gneerated by some of the helper functions. For instance, in Bottle's SimpleTemplate engine, you need to enclose the strings in {{! }}
.
bottle_utils.html
hsize
plur
strft
trunc
yesno
tag
link_other(label, url, path, wrapper=lambda l, kw: l,*kwargs)
vinput
varea
vcheckbox
vselect
form
QueryDict
add_qparam
set_qparam
del_qparam
urlquote
urlunquote
quote_dict
quoted_url