diff --git a/docs/extensions.rst b/docs/extensions.rst index 2445131..86086db 100644 --- a/docs/extensions.rst +++ b/docs/extensions.rst @@ -129,11 +129,10 @@ are the asynchronous versions of these two methods. The default location from where templates are loaded is the *templates* subdirectory. This location can be changed with the -:func:`init_templates ` function:: +:func:`Template.initialize ` class +method:: - from microdot.utemplate import init_templates - - init_templates('my_templates') + Template.initialize('my_templates') Using the Jinja Engine ^^^^^^^^^^^^^^^^^^^^^^ @@ -174,17 +173,15 @@ method, which returns a generator instead of a string. The default location from where templates are loaded is the *templates* subdirectory. This location can be changed with the -:func:`init_templates ` function:: +:func:`Template.initialize ` class method:: - from microdot.jinja import init_templates + Template.initialize('my_templates') - init_templates('my_templates') - -The ``init_templates()`` function also accepts ``enable_async`` argument, which +The ``initialize()`` method also accepts ``enable_async`` argument, which can be set to ``True`` if asynchronous rendering of templates is desired. If this option is enabled, then the -:func:`render_async() ` and -:func:`generate_async() ` methods +:func:`render_async() ` and +:func:`generate_async() ` methods must be used. .. note:: diff --git a/examples/templates/jinja/async_template.py b/examples/templates/jinja/async_template.py index 842adb2..b686763 100644 --- a/examples/templates/jinja/async_template.py +++ b/examples/templates/jinja/async_template.py @@ -1,7 +1,7 @@ from microdot import Microdot, Response -from microdot.jinja import template, init_templates +from microdot.jinja import Template -init_templates('templates', enable_async=True) +Template.initialize('templates', enable_async=True) app = Microdot() Response.default_content_type = 'text/html' @@ -11,7 +11,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return await template('index.html').render_async(name=name) + return await Template('index.html').render_async(name=name) if __name__ == '__main__': diff --git a/examples/templates/jinja/bootstrap.py b/examples/templates/jinja/bootstrap.py index a3005e0..d836224 100644 --- a/examples/templates/jinja/bootstrap.py +++ b/examples/templates/jinja/bootstrap.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.jinja import template +from microdot.jinja import Template app = Microdot() Response.default_content_type = 'text/html' @@ -7,12 +7,12 @@ Response.default_content_type = 'text/html' @app.route('/') async def index(req): - return template('page1.html').render(page='Page 1') + return Template('page1.html').render(page='Page 1') @app.route('/page2') async def page2(req): - return template('page2.html').render(page='Page 2') + return Template('page2.html').render(page='Page 2') if __name__ == '__main__': diff --git a/examples/templates/jinja/hello.py b/examples/templates/jinja/hello.py index 3fc5d98..a449cff 100644 --- a/examples/templates/jinja/hello.py +++ b/examples/templates/jinja/hello.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.jinja import template +from microdot.jinja import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/jinja/hello_asgi.py b/examples/templates/jinja/hello_asgi.py index 6057880..01f8fe4 100644 --- a/examples/templates/jinja/hello_asgi.py +++ b/examples/templates/jinja/hello_asgi.py @@ -1,5 +1,5 @@ from microdot.asgi import Microdot, Response -from microdot.jinja import template +from microdot.jinja import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/jinja/hello_wsgi.py b/examples/templates/jinja/hello_wsgi.py index a38fca8..15cc683 100644 --- a/examples/templates/jinja/hello_wsgi.py +++ b/examples/templates/jinja/hello_wsgi.py @@ -1,5 +1,5 @@ from microdot.wsgi import Microdot, Response -from microdot.jinja import template +from microdot.jinja import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/jinja/streaming.py b/examples/templates/jinja/streaming.py index b066a94..b61f5c4 100644 --- a/examples/templates/jinja/streaming.py +++ b/examples/templates/jinja/streaming.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.jinja import template +from microdot.jinja import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').generate(name=name) + return Template('index.html').generate(name=name) if __name__ == '__main__': diff --git a/examples/templates/utemplate/async_template.py b/examples/templates/utemplate/async_template.py index df0f7e3..e473173 100644 --- a/examples/templates/utemplate/async_template.py +++ b/examples/templates/utemplate/async_template.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return await template('index.html').render_async(name=name) + return await Template('index.html').render_async(name=name) if __name__ == '__main__': diff --git a/examples/templates/utemplate/bootstrap.py b/examples/templates/utemplate/bootstrap.py index ca798c9..2e2b837 100644 --- a/examples/templates/utemplate/bootstrap.py +++ b/examples/templates/utemplate/bootstrap.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -7,12 +7,12 @@ Response.default_content_type = 'text/html' @app.route('/') async def index(req): - return template('page1.html').render(page='Page 1') + return Template('page1.html').render(page='Page 1') @app.route('/page2') async def page2(req): - return template('page2.html').render(page='Page 2') + return Template('page2.html').render(page='Page 2') if __name__ == '__main__': diff --git a/examples/templates/utemplate/hello.py b/examples/templates/utemplate/hello.py index 7615dda..df3c582 100644 --- a/examples/templates/utemplate/hello.py +++ b/examples/templates/utemplate/hello.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/utemplate/hello_asgi.py b/examples/templates/utemplate/hello_asgi.py index ef901a3..21c45db 100644 --- a/examples/templates/utemplate/hello_asgi.py +++ b/examples/templates/utemplate/hello_asgi.py @@ -1,5 +1,5 @@ from microdot.asgi import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/utemplate/hello_wsgi.py b/examples/templates/utemplate/hello_wsgi.py index 97646fc..930d914 100644 --- a/examples/templates/utemplate/hello_wsgi.py +++ b/examples/templates/utemplate/hello_wsgi.py @@ -1,5 +1,5 @@ from microdot.wsgi import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').render(name=name) + return Template('index.html').render(name=name) if __name__ == '__main__': diff --git a/examples/templates/utemplate/streaming.py b/examples/templates/utemplate/streaming.py index 6595c62..9a6d6d4 100644 --- a/examples/templates/utemplate/streaming.py +++ b/examples/templates/utemplate/streaming.py @@ -1,5 +1,5 @@ from microdot import Microdot, Response -from microdot.utemplate import template +from microdot.utemplate import Template app = Microdot() Response.default_content_type = 'text/html' @@ -10,7 +10,7 @@ async def index(req): name = None if req.method == 'POST': name = req.form.get('name') - return template('index.html').generate(name=name) + return Template('index.html').generate(name=name) if __name__ == '__main__': diff --git a/src/microdot/jinja.py b/src/microdot/jinja.py index 95a4ded..0e1a976 100644 --- a/src/microdot/jinja.py +++ b/src/microdot/jinja.py @@ -3,36 +3,37 @@ from jinja2 import Environment, FileSystemLoader, select_autoescape _jinja_env = None -def init_templates(template_dir='templates', enable_async=False, **kwargs): - """Initialize the templating subsystem. - - :param template_dir: the directory where templates are stored. This - argument is optional. The default is to load templates - from a *templates* subdirectory. - :param enable_async: set to ``True`` to enable the async rendering engine - in Jinja, and the ``render_async()`` and - ``generate_async()`` methods. - :param kwargs: any additional options to be passed to Jinja's - ``Environment`` class. - """ - global _jinja_env - _jinja_env = Environment( - loader=FileSystemLoader(template_dir), - autoescape=select_autoescape(), - enable_async=enable_async, - **kwargs - ) - - class Template: """A template object. :param template: The filename of the template to render, relative to the configured template directory. """ + @classmethod + def initialize(cls, template_dir='templates', enable_async=False, + **kwargs): + """Initialize the templating subsystem. + + :param template_dir: the directory where templates are stored. This + argument is optional. The default is to load + templates from a *templates* subdirectory. + :param enable_async: set to ``True`` to enable the async rendering + engine in Jinja, and the ``render_async()`` and + ``generate_async()`` methods. + :param kwargs: any additional options to be passed to Jinja's + ``Environment`` class. + """ + global _jinja_env + _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 - init_templates() + self.initialize() #: The name of the template self.name = template self.template = _jinja_env.get_template(template) diff --git a/src/microdot/utemplate.py b/src/microdot/utemplate.py index 34694d9..16d0398 100644 --- a/src/microdot/utemplate.py +++ b/src/microdot/utemplate.py @@ -3,30 +3,32 @@ from utemplate import recompile _loader = None -def init_templates(template_dir='templates', loader_class=recompile.Loader): - """Initialize the templating subsystem. - - :param template_dir: the directory where templates are stored. This - argument is optional. The default is to load templates - from a *templates* subdirectory. - :param loader_class: the ``utemplate.Loader`` class to use when loading - templates. This argument is optional. The default is - the ``recompile.Loader`` class, which automatically - recompiles templates when they change. - """ - global _loader - _loader = loader_class(None, template_dir) - - class Template: """A template object. :param template: The filename of the template to render, relative to the configured template directory. """ + @classmethod + def initialize(cls, template_dir='templates', + loader_class=recompile.Loader): + """Initialize the templating subsystem. + + :param template_dir: the directory where templates are stored. This + argument is optional. The default is to load + templates from a *templates* subdirectory. + :param loader_class: the ``utemplate.Loader`` class to use when loading + templates. This argument is optional. The default + is the ``recompile.Loader`` class, which + automatically recompiles templates when they + change. + """ + global _loader + _loader = loader_class(None, template_dir) + def __init__(self, template): if _loader is None: # pragma: no cover - init_templates() + self.initialize() #: The name of the template self.name = template self.template = _loader.load(template) diff --git a/tests/test_jinja.py b/tests/test_jinja.py index 2d381fc..cc8e75b 100644 --- a/tests/test_jinja.py +++ b/tests/test_jinja.py @@ -2,10 +2,10 @@ import asyncio import sys import unittest from microdot import Microdot -from microdot.jinja import Template, init_templates +from microdot.jinja import Template from microdot.test_client import TestClient -init_templates('tests/templates') +Template.initialize('tests/templates') @unittest.skipIf(sys.implementation.name == 'micropython', @@ -49,7 +49,7 @@ class TestJinja(unittest.TestCase): self.assertEqual(res.body, b'Hello, foo!') def test_render_async_template_in_app(self): - init_templates('tests/templates', enable_async=True) + Template.initialize('tests/templates', enable_async=True) app = Microdot() @@ -62,10 +62,10 @@ class TestJinja(unittest.TestCase): self.assertEqual(res.status_code, 200) self.assertEqual(res.body, b'Hello, foo!') - init_templates('tests/templates') + Template.initialize('tests/templates') def test_generate_async_template_in_app(self): - init_templates('tests/templates', enable_async=True) + Template.initialize('tests/templates', enable_async=True) app = Microdot() @@ -78,4 +78,4 @@ class TestJinja(unittest.TestCase): self.assertEqual(res.status_code, 200) self.assertEqual(res.body, b'Hello, foo!') - init_templates('tests/templates') + Template.initialize('tests/templates') diff --git a/tests/test_utemplate.py b/tests/test_utemplate.py index 5784a5e..09a8f6b 100644 --- a/tests/test_utemplate.py +++ b/tests/test_utemplate.py @@ -2,9 +2,9 @@ import asyncio import unittest from microdot import Microdot from microdot.test_client import TestClient -from microdot.utemplate import Template, init_templates +from microdot.utemplate import Template -init_templates('tests/templates') +Template.initialize('tests/templates') class TestUTemplate(unittest.TestCase):