The Templater Module

class smacha.templater.Templater(template_dirs=[], include_comments=False, include_introspection_server=False)[source]

Main SMACHA template processor class.

The templater uses Jinja2 as its main engine for processing templates, but it also provides some additional features and functionality.

find_template_name(regex, template_env=None)[source]

Find a template name in the available template list using a regular expression.

Parameters:
  • regex (str) – A regular expression used to search the list of templates.
  • template_env (jinja2.environment) – Optionally specify the template environment. Defaults to current template_env member object.
Returns:

The first template name found or an empty string if no template is found.

Return type:

str

get_template_blocks(template_name)[source]

Get all template blocks.

Get a list of all blocks in a given template, either those defined directly in the template itself, or those defined in templates referenced via {% include %} or {% extends %} tags.

Parameters:template_name (str) – The name of the template.
Returns:The names of the template blocks.
Return type:set of str
get_template_refs(template_name)[source]

Get all templates referenced in a template.

Get a list of all templates referenced via {% include %} or {% extends %} tags in a given template.

Parameters:template_name (str) – The name of the template.
Returns:The names of the templates referenced by the template.
Return type:list of str
get_template_vars(template_name, context=None)[source]

Get all variables defined in a template.

Parameters:
  • template_name (str) – The name of the template.
  • context (dict or None) – The (optional) template context.
Returns:

The variables defined in the template.

Return type:

dict

list_templates()[source]

List the available templates.

Returns:A list of names of templates.
Return type:list of str
render(template_name, template_vars)[source]

Render code template.

Parameters:
  • template_name (str) – The name of the template to be rendered.
  • template_vars (dict) – The template variables.
Returns:

The rendered template code.

Return type:

str

render_all_blocks(template_name, template_vars)[source]

Render all blocks from code template.

Parameters:
  • template_name (str) – The name of the template.
  • template_vars (dict) – The template variables.
Returns:

The rendered code for each template block.

Return type:

dict

render_block(template_name, template_vars, target_block)[source]

Render specific block from code template.

Parameters:
  • template_name (str) – The name of the template.
  • template_vars (dict) – The template variables.
  • target_block (str) – The name of the block to be rendered.
Returns:

The rendered template block code.

Return type:

str

render_meta_block(template_name)[source]

Render meta block from code template.

Parameters:template_name (str) – The name of the template.
Returns:The rendered code for the template meta block.
Return type:str