from jinja2 import Environment, Undefined

def custom_sort(iterable, somearg):
    if iterable is None or isinstance(iterable, Undefined):
        return iterable
return iterable
env = Environment()
Custom jinja2 filter for iterator