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 as jy

ctor = jy.Constructor()

# Attention: tag name starts with "!"

# Add to default loader
yaml.add_constructor("!j2", ctor)
# or: Add to CLoader
yaml.add_constructor("!j2", ctor, yaml.CLoader)
# or: Add to SafeLoader
yaml.add_constructor("!j2", ctor, yaml.SafeLoader)
# or: Add to other Loaders ...

Attention

  • Custom YAML tag starts with "!". When we invoke yaml.add_constructor, the tag parameter MUST have a single "!" at the beginning.

  • Content of the tag MUST be text