Skip to content

Commit

Permalink
Expose the Jinja environment as Template.jinja_env
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Mar 2, 2025
1 parent 68a53a7 commit 953dd94
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/microdot/jinja.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from jinja2 import Environment, FileSystemLoader, select_autoescape

_jinja_env = None


class Template:
"""A template object.
:param template: The filename of the template to render, relative to the
configured template directory.
:param kwargs: any additional options to be passed to the Jinja
environment's ``get_template()`` method.
"""
#: The Jinja environment.
jinja_env = None

@classmethod
def initialize(cls, template_dir='templates', enable_async=False,
**kwargs):
"""Initialize the templating subsystem.
This method is automatically invoked when the first template is
created. The application can call it explicitly if custom options need
to be provided.
:param template_dir: the directory where templates are stored. This
argument is optional. The default is to load
templates from a *templates* subdirectory.
Expand All @@ -23,20 +30,19 @@ def initialize(cls, template_dir='templates', enable_async=False,
:param kwargs: any additional options to be passed to Jinja's
``Environment`` class.
"""
global _jinja_env
_jinja_env = Environment(
cls.jinja_env = Environment(
loader=FileSystemLoader(template_dir),
autoescape=select_autoescape(),
enable_async=enable_async,
**kwargs
)

def __init__(self, template):
if _jinja_env is None: # pragma: no cover
def __init__(self, template, **kwargs):
if self.jinja_env is None: # pragma: no cover
self.initialize()
#: The name of the template
#: The name of the template.
self.name = template
self.template = _jinja_env.get_template(template)
self.template = self.jinja_env.get_template(template, **kwargs)

def generate(self, *args, **kwargs):
"""Return a generator that renders the template in chunks, with the
Expand Down

0 comments on commit 953dd94

Please sign in to comment.