Compiler

Contains the main compilation function for driving template construction.

To avoid clashing with the built-in compile this method is called kompile.

knights.compiler.kompile(source, raw=False, filename='<compiler>', loader=None, **kwargs):)

Compiles a template from the provided source string.

If raw is True, the template class will be returned, not an instance.

Constructs a knights.parser.Parser class, loads the default tags and helpers, and builds a __call__ method for the class which is effectively:

return ''.join(str(x) for x in self._root(context))

where context is the sole argument to __call__.

Next, it calls parser.build_method(‘_root’) to consume the tokens.

If, after this, parser.parent is not None, it will remove the _root method, relying on the parent to provide one.

Next is calls parser.build_class(), wraps it in an ast.Module, and compiles it.

Finally it executes the code, putting the parser.parent and parser.helpers into the global context.

It returns the ‘Template’ object from the resulting global context.