bh_utils.template_funcs module

Some template functions.

For usage examples, see ./tests/test_template_funcs.py.

bh_utils.template_funcs.template_root_path(source_dir='src') str

Get a project template root path.

This method assumes the project has the following layout.

f:\project_name\
|
|-- ...
|
|-- src\
|   |
|   |-- project_name\
|       |
|       |-- ...
|       |
|       |-- templates\
|       |   |
|       |   |-- base_template.html
|       |   |
|       |   |-- auth\
|       |   |-- report\
|       |
|       |-- static\

The only possible variation is src\.

Parameters:

source_dir (str) – the project source directory.

Returns:

project template root path.

Return type:

str.

Return examples:
  • Windows: f:\project_name\{source_dir}\project_name\templates

  • Linux: /volume1/web/project_name/{source_dir}/project_name/templates

bh_utils.template_funcs.template_path(template_dir: str, source_dir='src')

Get a project template sub-path under the template root path.

Call to template_root_path() to get the project template root path, then append a sub-path to this root path.

Parameters:
  • template_dir (str) – template sub-path.

  • source_dir – the project source directory.

Returns:

a project template sub-path.

Return type:

str.

Return examples:
  • Windows: f:\project_name\{source_dir}\project_name\templates\{template_dir}

  • Linux: /volume1/web/project_name/{source_dir}/project_name/templates/{template_dir}

bh_utils.template_funcs.get_template(template_env: Environment, file_name: str) Template

Attempt to load a template.

Parameters:
  • template_env (jinja2.Environment) – an already prepared valid Jinja Environment class.

  • file_name (str) – a template file name to load.

  • logger – where to log possible exception to.

Returns:

jinja2.Template.