class documentation

class FunctionLoader(BaseLoader): (source)

View In Hierarchy

A loader that is passed a function which does the loading. The function receives the name of the template and has to return either a string with the template source, a tuple in the form ``(source, filename, uptodatefunc)`` or `None` if the template does not exist. >>> def load_template(name): ... if name == 'index.html': ... return '...' ... >>> loader = FunctionLoader(load_template) The `uptodatefunc` is a function that is called if autoreload is enabled and has to return `True` if the template is still up to date. For more details have a look at :meth:`BaseLoader.get_source` which has the same return value.

Method __init__ Undocumented
Method get_source Get the template source, filename and reload helper for a template. It's passed the environment and template name and has to return a tuple in the form ``(source, filename, uptodate)`` or raise a `TemplateNotFound` error if it can't locate the template.
Instance Variable load_func Undocumented

Inherited from BaseLoader:

Method list_templates Iterates over all templates. If the loader does not support that it should raise a :exc:`TypeError` which is the default behavior.
Method load Loads a template. This method looks up the template in the cache or loads one by calling :meth:`get_source`. Subclasses should not override this method as loaders working on collections of other loaders (such as :class:`PrefixLoader` or :class:`ChoiceLoader`) will not call this method but `get_source` directly.
Class Variable has_source_access Undocumented
def __init__(self, load_func): (source)

Undocumented

Parameters
load_func:t.Callable[[str], t.Optional[t.Union[str, t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]]]]Undocumented
def get_source(self, environment, template): (source)

Get the template source, filename and reload helper for a template. It's passed the environment and template name and has to return a tuple in the form ``(source, filename, uptodate)`` or raise a `TemplateNotFound` error if it can't locate the template. The source part of the returned tuple must be the source of the template as a string. The filename should be the name of the file on the filesystem if it was loaded from there, otherwise ``None``. The filename is used by Python for the tracebacks if no loader extension is used. The last item in the tuple is the `uptodate` function. If auto reloading is enabled it's always called to check if the template changed. No arguments are passed so the function must store the old state somewhere (for example in a closure). If it returns `False` the template will be reloaded.

Parameters
environment:EnvironmentUndocumented
template:strUndocumented
Returns
t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]Undocumented
load_func = (source)

Undocumented