palgen.template.jinja#

Module Contents#

class palgen.template.jinja.Template#
palgen.template.jinja.Template jinja2.environment.Environment jinja2.environment.Environment jinja2.environment.Environment->jinja2.environment.Environment linked_to jinja2.environment.Template jinja2.environment.Template jinja2.environment.Environment->jinja2.environment.Template environment jinja2.environment.Environment->jinja2.environment.Template environment jinja2.environment.TemplateModule jinja2.environment.TemplateModule jinja2.environment.TemplateModule->jinja2.environment.Template _module jinja2.utils.LRUCache jinja2.utils.LRUCache jinja2.utils.LRUCache->jinja2.environment.Environment cache jinja2.utils.LRUCache->jinja2.environment.Environment cache jinja2.utils.LRUCache->jinja2.environment.Environment cache palgen.template.jinja.Template palgen.template.jinja.Template palgen.template.jinja.Template palgen.template.jinja.Template->jinja2.environment.Template threading.lock threading.lock threading.lock->jinja2.utils.LRUCache _wlock

A compiled template that can be rendered.

Use the methods on Environment to create or load templates. The environment is used to configure how templates are compiled and behave.

It is also possible to create a template object directly. This is not usually recommended. The constructor takes most of the same arguments as Environment. All templates created with the same environment arguments share the same ephemeral Environment instance behind the scenes.

A template object should be considered immutable. Modifications on the object are not supported.

property module: TemplateModule#

The template as module. This is used for imports in the template runtime but is also useful if one wants to access exported template variables from the Python layer:

>>> t = Template('{% macro foo() %}42{% endmacro %}23')
>>> str(t.module)
'23'
>>> t.module.foo() == u'42'
True

This attribute is not available if async mode is enabled.

Return type:

TemplateModule

property is_up_to_date: bool#

If this variable is False there is a newer version available.

Return type:

bool

property debug_info: List[Tuple[int, int]]#

The debug info mapping.

Return type:

List[Tuple[int, int]]

__call__#
environment_class: Type[Environment]#
environment: Environment#
globals: MutableMapping[str, Any]#
name: str | None#
filename: str | None#
blocks: Dict[str, Callable[[jinja2.runtime.Context], Iterator[str]]]#
root_render_func: Callable[[jinja2.runtime.Context], Iterator[str]]#
static default_environment()#
Return type:

jinja2.Environment

classmethod from_code(environment, code, globals, uptodate=None)#

Creates a template object from compiled code and the globals. This is used by the loaders and environment to create a template object.

Parameters:
  • environment (Environment) –

  • code (types.CodeType) –

  • globals (MutableMapping[str, Any]) –

  • uptodate (Optional[Callable[[], bool]]) –

Return type:

Template

classmethod from_module_dict(environment, module_dict, globals)#

Creates a template object from a module. This is used by the module loader to create a template object.

New in version 2.4.

Parameters:
  • environment (Environment) –

  • module_dict (MutableMapping[str, Any]) –

  • globals (MutableMapping[str, Any]) –

Return type:

Template

render(*args, **kwargs)#

This method accepts the same arguments as the dict constructor: A dict, a dict subclass or some keyword arguments. If no arguments are given the context will be empty. These two calls do the same:

template.render(knights='that say nih')
template.render({'knights': 'that say nih'})

This will return the rendered template as a string.

Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

str

async render_async(*args, **kwargs)#

This works similar to render() but returns a coroutine that when awaited returns the entire rendered template string. This requires the async feature to be enabled.

Example usage:

await template.render_async(knights='that say nih; asynchronously')
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

str

stream(*args, **kwargs)#

Works exactly like generate() but returns a TemplateStream.

Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

TemplateStream

generate(*args, **kwargs)#

For very large templates it can be useful to not render the whole template at once but evaluate each statement after another and yield piece for piece. This method basically does exactly that and returns a generator that yields one item after another as strings.

It accepts the same arguments as render().

Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

Iterator[str]

async generate_async(*args, **kwargs)#

An async version of generate(). Works very similarly but returns an async iterator instead.

Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

AsyncIterator[str]

new_context(vars=None, shared=False, locals=None)#

Create a new Context for this template. The vars provided will be passed to the template. Per default the globals are added to the context. If shared is set to True the data is passed as is to the context without adding the globals.

locals can be a dict of local variables for internal usage.

Parameters:
  • vars (Optional[Dict[str, Any]]) –

  • shared (bool) –

  • locals (Optional[Mapping[str, Any]]) –

Return type:

jinja2.runtime.Context

make_module(vars=None, shared=False, locals=None)#

This method works like the module attribute when called without arguments but it will evaluate the template on every call rather than caching it. It’s also possible to provide a dict which is then used as context. The arguments are the same as for the new_context() method.

Parameters:
  • vars (Optional[Dict[str, Any]]) –

  • shared (bool) –

  • locals (Optional[Mapping[str, Any]]) –

Return type:

TemplateModule

async make_module_async(vars=None, shared=False, locals=None)#

As template module creation can invoke template code for asynchronous executions this method must be used instead of the normal make_module() one. Likewise the module attribute becomes unavailable in async mode.

Parameters:
  • vars (Optional[Dict[str, Any]]) –

  • shared (bool) –

  • locals (Optional[Mapping[str, Any]]) –

Return type:

TemplateModule

get_corresponding_lineno(lineno)#

Return the source line number of a line number in the generated bytecode as they are not in sync.

Parameters:

lineno (int) –

Return type:

int

__repr__()#

Return repr(self).

Return type:

str