How can I send variables to Jinja template from a Flask decorator? How can I send variables to Jinja template from a Flask decorator? flask flask

How can I send variables to Jinja template from a Flask decorator?


I'm going to propose something even simpler than using a decorator or template method or anything like that:

def render_sidebar_template(tmpl_name, **kwargs):    (var1, var2, var3) = generate_sidebar_data()    return render_template(tmpl_name, var1=var1, var2=var2, var3=var3, **kwargs)

Yup, just a function. That's all you really need, isn't it? See this Flask Snippet for inspiration. It's essentially doing exactly the same sort of thing, in a different context.


You can use a context processor (http://flask.pocoo.org/docs/api/#flask.Flask.context_processor):

def include_sidebar_data(fn):    @blueprint.context_processor    def additional_context():        # this code work if endpoint equals to view function name        if request.endpoint != fn.__name__:            return {}         var1, var2, var3 = generate_sidebar_data()        return {            'var1': var1,            'var2': var2,            'var3': var3,        }    return fn@blueprint.route('/')@include_sidebar_datadef frontpage():    return render_template('template.html')

UPD: I like the next example more and it is better if the decorator is used for several view functions:

sidebar_data_views = []def include_sidebar_data(fn):    sidebar_data_views.append(fn.__name__)    return fn@blueprint.context_processordef additional_context():    # this code work if endpoint equals to view function name    if request.endpoint not in sidebar_data_views:        return {}     var1, var2, var3 = generate_sidebar_data()    return {        'var1': var1,        'var2': var2,        'var3': var3,    }@blueprint.route('/')@include_sidebar_datadef frontpage():    return render_template('template.html')


You could create a decorator function like this:

def include_sidebar_data(fn):    template_name = fn()    var1, var2, var3 = generate_sidebar_data()    def wrapped():        return render_template(template_name, var1=var2, var2=var2)    return wrapped@blueprint.route('/')@include_sidebar_datadef frontpage():    return 'template.html'