While jinja2 does not have a way to handle this by default, there is a workaround.
Since try is not supported in the template language, we need a helper function defined in Python, like this:
def handle_catch(caller, on_exception):
try:
return caller()
except:
return on_exception
This method is injected into the template engine, either via the Environment.globals or when calling the render method. In this example it is injected via the render method.
my_template.render(handle_catch=handle_catch)
In the template itself it is then possible to define a macro:
{% macro catch(on_exception) %}
{{ handle_catch(caller, on_exception) }}
{% endmacro %}
And this can then be used as:
{% for item in items %}
{% call catch('') %}
{{ item | custom_urlencode_filter }}
{% endcall %}
{% endfor %}
Notes:
- The caller method is provided by the jinja2, and this is a function that renders the code between
{% call ... %} and {% endcall %}
- on_exception can be used to provide an alternative text in case of exceptions, but in this case we just use an empty string.
{% try %}in jinja (or equivalent). As far as I see it, your options are to write an exception safe version ofurlencodeand use that as your filter, or to sanitize the input before passing it to jinja2. - mgilson