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 %}
Related
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 %}`
I want to check if a condition in the linked database is true and then execute some code but I am getting error such as
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'
{% for prod in prod %}
{% if {{prod.sh}} is 1 %}
<pre>Lines to come if true</pre>
{% endif %}
{% endfor %}
The extensive Jinja2 documentation can be found at https://jinja.palletsprojects.com/en/2.11.x/templates/
About your code - I spot two problems.
You should not use for prod in prod - rather, but something like for product in products, ie name them differently.
You do not have to put angle brackets around prod.sh. You would do this only when the variable is referenced directly within the HTML code.
So a working code could look like:
{% for prod in prod %}
{% if prod.sh == 1 %}
<pre>Lines to come if true</pre>
{% endif %}
{% endfor %}
I'm trying to make a template which iterates over a list to check if a string exists. if it does then something happens, if the string isn't in the list, something else should happen. The issue is the result is repeated for all the lines in the list, which i do not want.
e.g.
The list is some simple yaml
LIST:
- A
- B
- C
Jinja looks like
{% for line in mylist %}
{% if 'A' in line %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}
{% endfor %}
So when 'A' matches i get this
A Was found in the list
A Was not found
A Was not found
And when 'A' does not match i get this:
A Was not found
A Was not found
A Was not found
Basically i need to stop it iterating over the list and just do one thing if it matches or one thing if it doesn't match
So if 'A' matches i need it to just do
A was found
If 'A' doesn't match i need it to just do A was not found
Use some kind of flag variable:
{% set ns = namespace(found=false) %}
{% for line in mylist %}
{% if 'A' in line %}
{% set ns.found=true %}
{% endif %}
{% endfor %}
{% if ns.foo %}
{{ A }} Was found in the list
{% else %}
{{ A }} Was not found
{% endif %}
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)
I want to display keywords and description automatically according to the category of the current post. I've used the following code, but it doesn't worked.
{% if page.categories = "category" %}
{% else %}
{% endif %}
But while using {% page.categories %} it is echoing out the category name correctly. Here are my two doubts:
How can I compare the current post's category?
Are {{ }} and {% %} are same here?
It should look like the following:
{% if page.categories == 'some-name' %}
Hei I am in some-name category
{% else %}
No I am not in some-name category
{% endif %}
2.
No, {{ }} and {% %} are not the same. {{ }} is used for echoing stuff, while {% %} is used for logic expressions and arguments.