























Apache Airflow uses Jinja templates for variable expansion and templating.
Whenever you write a DAG, you can use {{ }} blocks to expand variable names,
often using Airflow configurations or macros to fill in the values. While that
is great, you get a problem when you have Jinja templates inside your code that
has nothing to do with Airflow.
At those times, you should probably note that Jinja supports custom variable start and stop markers.
| |
This uses the normal markers that Jinja uses, just like many other templating languages. However, you might want to use other markers if you’re using this code block within the context of an Airflow DAG.
Then, you’d use:
| |
If you’d like to know more about this, you should look at the official Jinja2 documentation on the Template class.
Note
You can use these to override the need to implement your own hacks for nested template variables. This will even help with Flask or Django templates. Note that you probably shouldn’t implement too much of your logic within a template, but it is useful to know that this exists.
Warning
Another way of doing this is to use
jinja2.Undefined
so that Template.Render ignores your undefined values, so that the values can
be filled in later.
While this works, you shouldn’t use something that’s implemented in order to help you debug code, and you should instead use the provided APIs to do this.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。