jinja2 string comparison when one value is null? - jinja2

Jinja2 template code:
{% if u.user_name == u.user_email %}
{{ u.user_email }}
{% else %}
{{ u.user_name }}<br />
{{ u.user_email }}
{% endif %}
Usually the data source sets the user_name field to user_email if there is no separate name. However, sometimes there is nothing defined for the user_name field at all.
The problem is that in the Jinja2 code above, if the user_name field is undefined / null, the test above evaluates to false and both lines get spit out — one of which is blank.
I'm not sure how to properly write this test syntax. I've read the documentation and have tried approaches like Convert integer to string Jinja and compare two variables in jinja2 template. I guess I have a problem with data types, I'm just not sure how to write this syntax so that it works.
Any help is appreciated!

You want is None... try this
{% if (u.user_name == None or u.user_name == '') and u.user_name == u.user_email %}
{{ u.user_email }}
{% else %}
{{ u.user_name }}<br />
{{ u.user_email }}
{% endif %}
Hope this helps!

Related

How can I use {{this}} as a parameter in a dbt jinja macro?

How can I pass the name of the current model using {{this}} as a parameter for a macro in a config function?
I have tried a couple of options and none of them works.
model/Table1.sql
{{ config(post_hook= calculate_test("{{this}}") ) }}
macro/calculate_test.sql
{% macro calculate_test(tableN) %}
{%- set tableName = tableN -%}
{% set sql %}
SELECT
COUNT(*) as cnt
FROM {{ tableName }}
{% endset %}
{% set results = run_query(sql) %}
{% endmacro %}
The error is:
You’re almost there! Only thing is that you’ve got extra curly brackets and misplaced quotes. The entire config block is Jinja so you don’t need the curly brackets around ‘this’.
{{ config(post_hook= “calculate_test(this)” ) }}

Using if block in dbt models

Apologies for asking dumb question. But I tried many different approaches but none of them seems to work.
I have a requirement to select data from 2 different tables based on the variable. I am trying to do that in dbt models with if statement but it doesn't seem to work.
Model looks something like thins:
SELECT
*
FROM
{% if enable_whitelisting == 'true' %}
{{ ref('accounts_whitelisted') }} accounts
{% else %}
{{ ref('accounts') }} accounts
{% endif %}
Any help is appreciated.
Thanks in advance.
I got this working eventually. Have to put the variable name within the var()
SELECT
*
FROM
{% if var('enable_whitelisting') == 'true' %}
{{ ref('accounts_whitelisted') }} accounts
{% else %}
{{ ref('accounts') }} accounts
{% endif %}

Jinja2 - how to assign value to a variable based on if else condition?

I want to do something really simple in Jinja2 - if a>100 then do nothing else let b = 5. Here's my attempt but it does not work:
{% if a|float>100 %}
{% else %}
{% set b=5 %}
{% endif %}
I got an error like this:
jinja2.exceptions.TemplatesyntaxError: expected token 'end of statement block', got '%'
I couldn't find the correct syntax from Jinja2 documentation, so please kindly help. Thanks!
If nothing happens if the first condition is true, why don't you go straight to the second one?
How about:
{% if ( a|float>100 ) == false %}
{% set b=5 %}
{% endif %}`

Jinja "==" (compare condition) doesn't work?

while writing an application in django, I've encountered a problem. I want to make page-number links, with current page not being a link. So in template I do this:
{% for i in pages %}
{% if i == curr_page %} {{ i }}
{% else %} {{ i }}
{% endif %}
Only problem? Jinja doesn't seem to notice two numbers being equal. I've changed the 2nd line to {% if i != curr_page %} {{i}}!={{curr_page}} and got "... 5!=6 6!=6 7!=6 ...".
What should I do?
Because they are not of same data type. In your view, cast them to int before passing to context dict.
pages = list(map(int, pages))
curr_page = int(curr_page)

Liquid shorthands?

Are there any liquid shorthands? For example I'm trying to express this in a more concise way:
{% if job.offsite %}}
{{job.offsite}}
{{% else %}}
{{ job.url }}
{% endif %}
For this particular example, you could write:
{{ job.offsite | default: job.url }}
The value provided to the default filter will be used if the left side of the expression is false or nil.