python - Escaping string and then adding html tags in jinja -
i have string want escape , add specific html tags before printing in jinja. i'm trying do:
{{ user.info | e | nl2br | safe }}
so want convert new lines <br/>
escape every html tag provided user. doesn't seem work, <br/>
tags getting escaped. how can achieve such behavior in jinja?
okay, found solution, seems escape returns markup object, calling safe on doesn't help. had define custom filter:
@app.template_filter('escape_custom') def escape_custom(string): string = str(jinja2.escape(string)) return jinja2.markup(string.replace('\n', '<br/>\n'))
Comments
Post a Comment