Home > Mobile >  dbt jinja "elif" functionality?
dbt jinja "elif" functionality?

Time:10-08

According to the jinja documentation, a tag exists for the else if case within an if and else flow: Jinja Control Strucutures/IF

Looking for a way to do this within a dbt macro something like:

my_macro.sql

-- macros/my_macro.sql
{% macro my_macro() %}
  {% if target.name == 'default' %}
      select 'A' as my_letter;
  {% elif target.name == 'dev' %}
      select 'B' as my_letter;
  {% elif target.name == 'qa' %}
      select 'C' as my_letter;
  {% elif target.name == 'prod' %}
      select 'D' as my_letter;
  {% else %}
     select 1; -- hooks will error if they don't have valid SQL in them, this handles that!
  {% endif %}

{% endmacro %}

If the elif tag or equivalent is unavailable, what are my options? Something like the below only?

-- macros/my_macro.sql
{% macro my_macro() %}
  
  {% if target.name == 'default' %}
    select 'A' as my_letter;
  {% else %}
    select 1;
  {% endif %}
  
  {% if target.name == 'dev' %}
    select 'B' as my_letter;
  {% else %}
    select 1;
  {% endif %}
  
  {% if target.name == 'qa' %}
    select 'C' as my_letter;
  {% else %}
    select 1;
  {% endif %}
  
  {% if target.name == 'prod' %}
    select 'D' as my_letter;
  {% else %}
    select 1;
  {% endif %}

{% endmacro %}

CodePudding user response:

I often use elif in dbt jinja without issue. is there a reason you don't have this available?

CodePudding user response:

Disregard the question above - it was an indentation issue.

{% elif %} tags work fine.

  • Related