How to test for a List in Jinja2?

后端 未结 4 1177
生来不讨喜
生来不讨喜 2021-01-03 18:33

As far as I can see, there is no way to test if an object is a List instance in Jinja2.

Is that correct and has anyone implemented a custom test/extensio

4条回答
  •  别那么骄傲
    2021-01-03 19:11

    You can easily do this whit a custom filter in jinja2.

    First create you test method:

    def is_list(value):
        return isinstance(value, list)
    

    And add it as an custom filter:

    j = jinja2.Jinja2(app)
    j.environment.filters.update({
            'is_list': is_list,
    })
    

提交回复
热议问题