Creating the template renderer

When generating a simple HTML template in Jinja2, the following three steps are required:

  • Creating a template environment
  • Specifying the template
  • Rendering the template

With these three steps, it's important to identify which parts are never subject (or at least extremely unlikely) to change while our application is running... and which are. Keep this in mind as I explain through the following code.

In your dependencies directory, add a new file, jinja2.py and start with the following code:

from jinja2 import Environment, PackageLoader, select_autoescape class TemplateRenderer: def __init__(self, package_name, template_dir): self.template_env = Environment( loader=PackageLoader(package_name, template_dir), ...

Get Python Programming Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.