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_filepath (absolute or relative path to the template file) and/orpaths, 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 stringctx (
Optional[dict[str,Any]]) – Context dict needed to instantiate templatespaths (
Optional[Sequence[Union[str,Path]]]) – Template search pathsfilters (
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 filectx (
Optional[dict[str,Any]]) – Context dict needed to instantiate templatesfilters (
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 templatespaths (
Optional[Sequence[Union[str,Path]]]) – Template search pathsask (
bool) – Ask user for missing template location if Truefilters (
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 (
str|None) – Absolute or relative path to the template filecontent (
str|None) – Template content (str)options (
Any) – Keyword options passed torender()defined above.
- Return type:
str|None- Returns:
Compiled result (str) or None