jinja2.BaseLoader

class jinja2.BaseLoader[source]

Baseclass for all loaders. Subclass this and override get_source to implement a custom loading mechanism. The environment provides a get_template method that calls the loader’s load method to get the Template object.

A very basic example for a loader that looks up templates on the file system could look like this:

from jinja2 import BaseLoader, TemplateNotFound
from os.path import join, exists, getmtime

class MyLoader(BaseLoader):

    def __init__(self, path):
        self.path = path

    def get_source(self, environment, template):
        path = join(self.path, template)
        if not exists(path):
            raise TemplateNotFound(template)
        mtime = getmtime(path)
        with file(path) as f:
            source = f.read().decode('utf-8')
        return source, path, lambda: mtime == getmtime(path)

Methods

get_source(environment, template) Get the template source, filename and reload helper for a template.
list_templates() Iterates over all templates.
load(environment, name[, globals]) Loads a template.

Attributes

has_source_access if set to False it indicates that the loader cannot provide access