Skip to content

Commit

Permalink
Addressed some inadvertent mistakes in the template extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 23, 2023
1 parent f38d6d7 commit bd18ceb
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 84 deletions.
19 changes: 8 additions & 11 deletions docs/extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <microdot.utemplate.init_templates>` function::
:func:`Template.initialize <microdot.utemplate.Template.initialize>` class
method::

from microdot.utemplate import init_templates

init_templates('my_templates')
Template.initialize('my_templates')

Using the Jinja Engine
^^^^^^^^^^^^^^^^^^^^^^
Expand Down Expand Up @@ -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 <microdot.utemplate.init_templates>` function::

from microdot.jinja import init_templates
:func:`Template.initialize <microdot.jinja.Template.initialize>` class method::

init_templates('my_templates')
Template.initialize('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() <microdot.utemplate.Template.render_async>` and
:func:`generate_async() <microdot.utemplate.Template.generate_async>` methods
:func:`render_async() <microdot.jinja.Template.render_async>` and
:func:`generate_async() <microdot.jinja.Template.generate_async>` methods
must be used.

.. note::
Expand Down
6 changes: 3 additions & 3 deletions examples/templates/jinja/async_template.py
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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__':
Expand Down
6 changes: 3 additions & 3 deletions examples/templates/jinja/bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from microdot import Microdot, Response
from microdot.jinja import template
from microdot.jinja import Template

app = Microdot()
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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/jinja/hello.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/jinja/hello_asgi.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/jinja/hello_wsgi.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/jinja/streaming.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/utemplate/async_template.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
6 changes: 3 additions & 3 deletions examples/templates/utemplate/bootstrap.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
from microdot import Microdot, Response
from microdot.utemplate import template
from microdot.utemplate import Template

app = Microdot()
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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/utemplate/hello.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/utemplate/hello_asgi.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/utemplate/hello_wsgi.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/utemplate/streaming.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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__':
Expand Down
45 changes: 23 additions & 22 deletions src/microdot/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@
_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)
Expand Down
34 changes: 18 additions & 16 deletions src/microdot/utemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@
_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)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
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',
Expand Down Expand Up @@ -49,7 +49,7 @@ async def index(req):
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()

Expand All @@ -62,10 +62,10 @@ async def index(req):
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()

Expand All @@ -78,4 +78,4 @@ async def index(req):
self.assertEqual(res.status_code, 200)
self.assertEqual(res.body, b'Hello, foo!')

init_templates('tests/templates')
Template.initialize('tests/templates')
Loading

0 comments on commit bd18ceb

Please sign in to comment.