Flask + Jinja: Pass Data to a Base Template/All Templates

前端 未结 1 959
耶瑟儿~
耶瑟儿~ 2020-12-16 08:49

I have a method that returns data which is needed in my base template (content for a global footer).

How do either (1) pass a variable into the base template (which

相关标签:
1条回答
  • 2020-12-16 09:46

    From flask docs: Flask's Context Processors

    To inject new variables automatically into the context of a template, context processors exist in Flask. Context processors run before the template is rendered and have the ability to inject new values into the template context. A context processor is a function that returns a dictionary. The keys and values of this dictionary are then merged with the template context, for all templates in the app:

    Example from docs:

    @app.context_processor
    def inject_user():
        return dict(user=g.user)
    

    Note that this example uses the g variable, which is already accessible in templates.

    0 讨论(0)
提交回复
热议问题