Apply Jinja2 filter to every value in every rendered template
Apply a recursive function to your context dict before you pass it to Jinja2.
For example, this function escape every string inside a dict prepending a \
to specific characters.
def escape_markdown(data): if isinstance(data, dict): return {key: escape_markdown(val) for key, val in data.items()} elif isinstance(data, str): return re.sub(r'([\\*_])', r'\\\1', data) else: return data
Personally, I use a helper function to render my templates, which inserts some handy variables as well as being usable for what you need. I understand it's not exactly what you're asking for, but hopefully it will still be useful.
def render_response(self, _template, **context): # Renders a template and writes the result to the response. import json, time context['now']=int(time.time()) context['anything_you_want']=self.session['something'] for k in context: context[k] = json.dumps(context[k]) rv = self.jinja2.render_template(_template, **context) self.response.write(rv)