Templates Loader Plugins

Template loaders responsible for loading template text from various sources.

Base Template Loader

Reference name base

Base loader loads templates content in templates_dict dictionary using other loader plugins following this order:

  1. Check if template with given name already exists in templates_dict, use it if so

  2. Check if template name starts with ttr://, load it using ttr_template_loader

  3. If template name references file, load it using file_template_loader

  4. If templates is a directory load template content using dir_template_loader

  5. If templates referring to .xlsx file load all templates using xlsx_template_loader

On failure to load template file, base loader will log an error message and TTR will continue processing other data items.

ttr.plugins.templates.base_template_loader.load(template_name, templates_dict, templates, **kwargs)

Function to locate template file and return it’s content

Attributes

Parameters
  • template_name – (str) - name of template to load

  • templates – (str) - location of templates

  • templates_dict – (dict) - dictionary of to store template content in

  • kwargs – (dict) any additional arguments ignored

Returns

True on success and False on failure to load template

On success loads template content in templates_dict and returns True, on failure returns False.

File Template Loader

Reference name file

Loads template for rendering from file.

ttr.plugins.templates.file_template_loader.load(template_name, templates_dict, filepath=None, **kwargs)

Function to load template content from file path.

Parameters
  • template_name – (str) name of template to load, should point to file if no filepath argument provided

  • templates_dict – (str) dictionary to store template content in

  • filepath – (str) optional, path to file to open

  • kwargs – (dict) any additional arguments ignored

Returns

True on success and False on failure to load template

Directory Template Loader

Reference name dir

Loads template for rendering from file in directory

ttr.plugins.templates.dir_template_loader.load(template_name, templates_dict, templates, **kwargs)

Function to load template content from file in directory.

Parameters
  • template_name – (str) name of template to load, should point to file

  • templates_dict – (str) dictionary to store template content in

  • templates – (str) OS path to directory with template file

  • kwargs – (dict) any additional arguments ignored

Returns

True on success and False on failure to load template

TTR Template Loader

Reference name ttr

Loads templates for rendering from TTR package.

ttr.plugins.templates.ttr_template_loader.load(template_name, templates_dict, **kwargs)

Function to load template content from ttr://... path.

Parameters
  • template_name – (str) name of template to load

  • templates_dict – (str) dictionary to store template content in

  • kwargs – (dict) any additional arguments ignored

Returns

True on success and False on failure to load template

XLSX Template Loader

Plugin reference name: xlsx

Spreadsheet might contain multiple tabs with names starting with template, these tabs can contain templates to use for rendering data from other tabs. All the templates loaded line by line, template:{{ template_name }} lines used to identify end of previous and start of next template, where template_name used for referencing template.

Sample table that contains rendering templates, no headers required:

template:interface

interface {{ interface }}

description {{ description }}

template:logging

logging host {{ log_server }}

Above templates loaded in a dictionary:

{
    "interface": 'interface {{ interface }}\n'
                 '  description {{ description }}',
    "logging": 'logging host {{ log_server }}'
}

In this case templates referenced in data using interface and logging template names

ttr.plugins.templates.xlsx_template_loader.load(templates_dict, templates=None, sheet=None, **kwargs)

Function to load all templates from xlsx spreadsheets.

Parameters
  • templates – OS path to .xlsx file

  • sheet – openpyxl sheet object

  • templates_dict – (dict) dictionary of {template name: template content} to load templates in

  • kwargs – (dict) any additional arguments ignored

Returns

True on success and False on failure to load template