jinja_tag
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| JinjaConstructionError configz.yaml_tools.jinja_tag Error raised when Jinja template construction fails. |
🛈 DocStrings¶
MiniJinja template constructor for YAML loading.
JinjaConstructionError
¶
get_jinja_constructor
¶
Create a constructor that resolves !JINJA tags using a MiniJinja environment.
This constructor allows explicit template processing for values marked with the !JINJA tag in YAML documents. It supports scalar values, sequences, and mappings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
Environment | None
|
MiniJinja environment to use for template resolution. If None, returns the raw string without template processing. |
required |
Returns:
| Type | Description |
|---|---|
YAMLConstructor
|
A constructor function for the YAML loader. |
Example
from minijinja import Environment
import yaml
# Setup environment
env = Environment(globals={"name": "World"})
# Create loader with constructor
loader = yaml.Loader
loader.add_constructor('!JINJA', get_jinja_constructor(env))
# Use in YAML
yaml_text = '''
message: !JINJA "Hello {{ name }}!"
items: !JINJA
- "Item {{ name }}"
- "Value {{ name | upper }}"
nested:
value: !JINJA "{{ name | upper }}"
'''
data = yaml.load(yaml_text, Loader=loader)
Source code in src/configz/yaml_tools/jinja_tag.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
register_jinja_constructor
¶
Register the !JINJA tag constructor with a YAML loader class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader_class
|
type[Loader]
|
The YAML loader class to register the constructor with. |
required |
env
|
Environment | None
|
Optional MiniJinja environment for template resolution. |
None
|