Interpolation
What Loader(interpolate=...) renders with: a tplx engine through
TemplateInterpolator, ${a.b} references through VarsInterpolator, or an
object of your own with the Interpolator shape. See
Templating for interpolation and template files.
anycfg.Interpolator
Bases: Protocol
Renders template text found inside loaded values.
render is called with every string of the document and decides itself
whether the text is a template: text it does not render comes back as it is.
scope is the document's top-level names (plus env with
inject_env); reading a name that is itself a template renders it first.
render(text, scope)
text rendered; plain text as it is; one expression keeps its type.
Source code in src/anycfg/_templating.py
121 122 | |
anycfg.TemplateInterpolator(engine='jinja', *, sandbox=True, strict=False)
Interpolation through a tplx engine; sandboxed by default.
engine is a tplx engine's name or class ("jinja" by default,
"liquid", "go", "envsubst", ...). sandbox uses tplx's sandbox:
an untrusted value cannot reach Python internals and sees plain data — a
mapping or list as itself, any other non-plain value (a date) as its text —
and an engine tplx cannot sandbox is refused. strict makes an undefined
name raise instead of rendering empty. A template can include no file.
Compiled templates are cached per instance.
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
Without tplx, or the engine's own extra. |
TemplatesDisabledError
|
Under sandbox, for an engine tplx cannot sandbox (mako, Cheetah, ERB, EPP). |
ValueError
|
For a name no tplx engine has. |
Source code in src/anycfg/_templating.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
engine = engine
instance-attribute
engine_class = type(probe)
instance-attribute
sandbox = sandbox
instance-attribute
strict = strict
instance-attribute
render(text, scope)
tplx's eval: plain text back as it is, one expression with its
type, anything else rendered to text. The engine is the only syntax
check, and the template sees only the names it reads.
Source code in src/anycfg/_templating.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 | |
anycfg.VarsInterpolator(*, strict=False)
${name} and ${a.b.0} references; no expressions, no extra needed.
A reference is a dotted path into the document: each step a mapping key or
a list index. A value that is exactly one reference keeps the referenced
value's type; inside other text it is written with str (None as
nothing). $${ writes a literal ${. An undefined reference renders
empty (None alone), or raises TemplateError when strict. It runs no
code and reads no attribute, so it needs no sandbox.
Source code in src/anycfg/_templating.py
268 269 | |
engine = 'vars'
class-attribute
instance-attribute
strict = strict
instance-attribute
render(text, scope)
Source code in src/anycfg/_templating.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |