Adding A Custom Jinja2 Filter In Gae 1.6.0
I'd like to add filter to format my time and the best would be filters like django's timesince that automatically outputs the language of the i18n selected language, but first to m
Solution 1:
Following your example and Jinja2 docs I've added custom filter and it works.
Make sure that you use proper jinja2.Environment
instance for getting template and rendering:
env = jinja2.Environment(
loader=jinja2.FileSystemLoader(template_path))
env.filters['default_if_none'] = default_if_none # a function
tmpl = env.get_template(filename)
tmpl.render(**context)
Solution 2:
Because I was using a cached jinja2 environment as recommended here,
Kee's answer didn't work for me, but this one did.
Specifically, adding the filter when calling webapp2.WSGIApplication
myconfig = {}
myconfig['webapp2_extras.jinja2'] = {'template_path': ['templates','blog_posts'],
'filters': {'blog_filter': blog_filter}}
app = webapp2.WSGIApplication(_routes,
debug=True,
config = myconfig)
Post a Comment for "Adding A Custom Jinja2 Filter In Gae 1.6.0"