anyconfig.template.jinja2

anyconfig.template.jinja2 module.

Template rendering module for jinja2-based template config files.

anyconfig.template.jinja2.tmpl_env(paths=None, *, autoescape=True)

Get the template environment object from given paths.

Parameters:

paths (Optional[Sequence[Union[str, Path]]]) – A list of template search paths

Return type:

Environment

anyconfig.template.jinja2.make_template_paths(template_file, paths=None)

Make a template paths.

Make up a list of template search paths from given template_file path (absolute or relative path to the template file) and/or paths, a list of template search paths given by user or None.

NOTE: User-given ‘paths’ will take higher priority over a dir of template_file.

Return type:

list[Path]

anyconfig.template.jinja2.render_s(tmpl_s, ctx=None, paths=None, filters=None, *, autoescape=True)

Render a template as a str.

Compile and render given template string ‘tmpl_s’ with context ‘context’.

Parameters:
  • tmpl_s (str) – Template string

  • ctx (Optional[dict[str, Any]]) – Context dict needed to instantiate templates

  • paths (Optional[Sequence[Union[str, Path]]]) – Template search paths

  • filters (Optional[Iterable[Callable]]) – Custom filters to add into template engine

Return type:

str

Returns:

Compiled result (str)

>>> render_s("aaa") == "aaa"
True
>>> s = render_s("a = {{ a }}, b = '{{ b }}'", {"a": 1, "b": "bbb"})
>>> assert s == 'a = 1, b = "bbb"'
anyconfig.template.jinja2.render_impl(template_file, ctx=None, paths=None, filters=None, *, autoescape=True)

Render implementation.

Parameters:
  • template_file (Path) – Absolute or relative path to the template file

  • ctx (Optional[dict[str, Any]]) – Context dict needed to instantiate templates

  • filters (Optional[Iterable[Callable]]) – Custom filters to add into template engine

Return type:

str

Returns:

Compiled result (str)

anyconfig.template.jinja2.render(filepath, ctx=None, paths=None, *, ask=False, filters=None)

Compile and render template and return the result as a string.

Parameters:
  • template_file – Absolute or relative path to the template file

  • ctx (Optional[dict[str, Any]]) – Context dict needed to instantiate templates

  • paths (Optional[Sequence[Union[str, Path]]]) – Template search paths

  • ask (bool) – Ask user for missing template location if True

  • filters (Optional[Iterable[Callable]]) – Custom filters to add into template engine

Return type:

str

Returns:

Compiled result (str)

anyconfig.template.jinja2.try_render(filepath=None, content=None, **options)

Compile and render template and return the result as a string.

Parameters:
  • filepath (Optional[str]) – Absolute or relative path to the template file

  • content (Optional[str]) – Template content (str)

  • options – Keyword options passed to render() defined above.

Return type:

Optional[str]

Returns:

Compiled result (str) or None