Simply  Seth
Simply Seth

Reputation: 3496

How and where to define a custom jinja2 test

I know this is obvious to everybody but me.

But where and how do I define a custom jinja2 test?

I tried specifying in my flask:

import flask
import jinja2
from jinja2 import environment as env
from jinja2 import *

app = Flask(__name__)
app.config.from_object(__name__)
app = Flask(__name__)

# jinja2 filter
def isList(value):
  return isinstance(value, list)
env.tests['isList'] = isList

The resulting error is:

AttributeError: 'module' object has no attribute 'tests'

Upvotes: 2

Views: 893

Answers (1)

jonafato
jonafato

Reputation: 1605

The configured Jinja2 environment for Flask applications is app.jinja_env. If you change the last line in your code to

app.jinja_env.tests['isList'] = isList

it should work properly.

Upvotes: 5

Related Questions