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.
Standard Jinja Syntax
|
|
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.
Custom Delimiters
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.
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.