An option to keep undefined variables
pietervogelaar opened this issue · comments
It would be nice to support multi stage rendering as described at https://stackoverflow.com/a/53134416/948378.
from jinja2 import Environment, BaseLoader,DebugUndefined
rtemplate =Environment(loader=BaseLoader,undefined=DebugUndefined) \
.from_string("{{ a }} is defined, but {{ b}} is undefined")
print(rtemplate.render({"a":"a"}))
# The result is:
# a is defined, but {{ b }} is undefined
Hi @pietervogelaar , thank you for the feature request: you're not the first one asking about this :)
I wonder what the use case could be. Wouldn't it be possible in your case to merge the variables before rendering a template, instead of doing this multi-stage rendering? Isn't it prone to errors?
DebugUndefined
may indeed be an unorthodox way to implement multi-stage processing. But since this a feature already implemented in Jinja2, and exposing it adds practically no cruft to j2cli, I can't see why not to expose it if someone has a use for it.
Personally, I use this behaviour in its original scope: debugging. If you are trying to render and debug 100+ templates you can generate your output using DebugUndefined
. Then a grep -r '{{' output/
will help you quickly fix missing variables in the context, without going through multiple runs.
Note that I had implemented this (among other things) in #13. The patch is now outdated, but it should be straightforward to pull the relevant lines. I can send a new PR only with this part if you're too busy.