jinjyaml.constructor module

class jinjyaml.constructor.Constructor

Bases: object

Constructor class for Jinja2 template YAML tags.

When parsing YAML string, the class constructs template tags to Data objects.

Add the constructor to PyYAML Loader as below:

import yaml
import jinjyaml

constructor = jinjyaml.Constructor()

# Attention: tag name starts with "!"
yaml.add_constructor('!j2', constructor)
# or
yaml.add_constructor('!j2', constructor, yaml.CLoader)

Attention

  • Custom YAML tags start with "!".

    So, when we call yaml.add_constructor, the tag parameter MUST have a single "!" at the beginning.

  • Content of the tag MUST be text