I'm trying to write a multiline if statement to set a variable in jinja2. But I keep getting an error
the code:
{% set subjectName = subject.name %}
{% if(subjectName == ''):
subjectName = 'empty'
%}
{% endif %}
error:
jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'subjectName'
is multiline code not allowed?
You just put the %} in the wrong spot. This should work (I also took out the colon, I don't think that goes in here either):
{% set subjectName = subject.name %}
{% if(subjectName == '') %}
subjectName = 'empty'
{% endif %}
after searching a bit, this worked for me:
{% set subjectName = subject.name %}
{% if(subjectName == '') %}
{% set subjectName = 'empty' %}
{% endif %}
Related
I have big html document with various images with href and src.
I want to make an if statement based on the output of the src.
<img class="img-fluid d-block w-100" src="/static/assets/img/{{ LANGUAGE_CODE }}/actionFlow({% if form.status.value|slugify == '1'%}taak-toegewezen{% else %}{{form.status.value|slugify}}{%endif%}).png" id="workflowVisual">
Example outputs can be:
"/static/assets/img/en/actionFlow(taak-toegewezen).png"
"/static/assets/img/en/actionFlow(firststep).png"
Now, I want to create an if statement like so:
{% if src== "/static/assets/img/en/actionFlow(taak-toegewezen).png"}{{instance.reviewer2}}
{% else src== "/static/assets/img/en/actionFlow(firststep).png"}{{instance.reviewer1}}{%endif%}
How do I do this in HTML?
Best,
Rob
You seem to use django templating, which is not just HTML.
You can set variables using {% with variable = 1 %}:
{% if form.status.value|slugify == '1' %}
{% with SLUG = 'taak-toegewezen' %}
{% else %}
{% with SLUG = form.status.value|slugify %}
{% endif %}
{% with imgSrc = "/static/assets/img/"|addstr:LANGUAGE_CODE|addstr:"/actionFlow("|addstr:SLUG|addstr:").png" %}
And now it's way easier to use in a if statement.
{% if imgSrc == "/static/assets/img/en/actionFlow(taak-toegewezen).png" %}
{{instance.reviewer2 }}
{% else if imgSrc == "/static/assets/img/en/actionFlow(firststep).png" %}
{{instance.reviewer1 }}
{% else %}
Default value
{% endif %}
I am trying to put the result of a query in a variable but it doesn't work.
I am not sure what to do so it returns 0 as expected. Any ideas? I am using dbt and jinja.
With the below code the results_list variable is (Decimal('0'),))
MACRO
{% macro source_freshness(model, column_name) %}
{% set freshness_query %}
SELECT COUNT 0 AS count
{% endset %}
{% set results = run_query(freshness_query) %}
{% if execute %}
{% set results_list = results.columns[0].values() %}
{% else %}
{% set results_list = [] %}
{% endif %}
{{ return(results_list) }}
{% endmacro %}
call in a model:
{% set freshness_query_test = source_freshness(ref('model'),'date') %}
{% if count in freshness_query_test == 0 %}
do this
{% else %}
do that
{% endif %}
Thank you!
thanks for your help with this. I have not been able to find a direct answer but what I have done is to add the macro in a separate model, and then use the call statement logic in the shared answer Hi, how do we define select statement as a variable in dbt?
What is the best way to make custom language switcher to the site.
My problem is that I have a site with two language versions and my custom language switcher is a bit problematic I would say. It doesn't work in all pages for some reason and in my opinion is it coded in very complicated way. In our site the language versions are changed via url. For example mycompany.fi/ is the main language version and the mycompany.fi/en/ is for the english version of the site. Here is the code how I switch language currently by just changing the url. In the code it checks the url and also if the page has translated_content. After that I put the site_language and other_language to links so by pressing the link it changes the language.
{% if absolute_url is string_containing "/en-us/" %}
{% set site_language = 'en' %}
{% set other_language = 'fi' %}
{% if content.translated_content['fi-fi'] %}
{% set other_language_url = '/' + content.translated_content['fi-fi'].slug %}
{% elif content.translated_content['fi'] %}
{% set other_language_url = '/' + content.translated_content['fi'].slug %}
{% else %}
{% set other_language_url = '#' %}
{% endif %}
{% elif absolute_url is string_containing "/en/" %}
{% set site_language = 'en' %}
{% set other_language = 'fi' %}
{% if content.translated_content['fi-fi'] %}
{% set other_language_url = '/' + content.translated_content['fi-fi'].slug %}
{% elif content.translated_content['fi'] %}
{% set other_language_url = '/' + content.translated_content['fi'].slug %}
{% else %}
{% set other_language_url = '#' %}
{% endif %}
{% else %}
{% set site_language = 'fi' %}
{% set other_language = 'en' %}
{% if content.translated_content['en-us'] %}
{% set other_language_url = '/' + content.translated_content['en-us'].slug %}
{% elif content.translated_content['en'] %}
{% set other_language_url = '/' + content.translated_content['en'].slug %}
{% else %}
{% set other_language_url = '#' %}
{% endif %}
{% endif %}
I'm wondering is there any other way to make this happen because for me this seems very complicated solution. I would like to know if there is some global variable or something what defines the current language used in the page.
I am trying to iteratively concatenate a string to build url params with a 'for' loop, but I believe I am having scoping issues.
The output should be: url_param = "&query_param=hello&query_param=world"
array_of_objects = [{'id':'hello'},{'id':'world'}]
{% set url_param = "" %}
{% set array_of_ids = array_of_objects|map(attribute='id')|list%} // correctly returns [1,2]
{% for id in array_of_ids %}
{% set param = '&query_param='~id %}
{% set url_param = url_param~param %}
{% endfor %}
//url_param is still an empty string
I also tried namespace(), but to no avail:
{% set ns = namespace() %}
{% set ns.output = '' %}
{% set array_of_ids = array_of_objects|map(attribute='id')|list%} // correctly returns [1,2]
{% for id in array_of_ids %}
{% set param = '&industries='~id%}
{% set ns.output = ns.output~param %}
{% endfor %}
//ns.output returns namespace
That is indeed a scope issue. One "hacky" way of dealing with this is using a list that you append to like so:
{% set array_of_objects = [{'id':'hello'},{'id':'world'}] %}
{% set array_of_ids = array_of_objects|map(attribute='id')|list%}
{{ array_of_ids|pprint }} {# output: ['hello', 'world'] #}
{% set ids = [] %} {# Temporary list #}
{% for id in array_of_ids %}
{% set param = '&query_param='~id %}
{% set url_param = url_param~param %}
{{ ids.append(url_param) }}
{% endfor %}
{{ ids|pprint }} {# output: [u'&query_param=hello', u'&query_param=world'] #}
{{ ids|join|pprint }} {# output: "&query_param=hello&query_param=world" #}
The above gets you what you need, but for this specific example I would take a look at using jinja's join filter. It's more declarative and feels a little less hacky.
{% set array_of_objects = [{'id':'hello'},{'id':'world'}] %}
{# set to a variable #}
{% set query_string = "&query_param=" ~ array_of_objects|join("&query_param=", attribute="id") %}
{{ query_string|pprint }}
{# output: u'&query_param=hello&query_param=world' #}
{# or just use it inline #}
{{ "&query_param=" ~ array_of_objects|join("&query_param=", attribute="id") }}
You should change the initialization of your namespace.
Here is an example from the docs that will help you out:
{% set ns = namespace(found=false) %}
{% for item in items %}
{% if item.check_something() %}
{% set ns.found = true %}
{% endif %}
* {{ item.title }}
{% endfor %}
Found item having something: {{ ns.found }}
I try to assign to a variable a true or false value depending on whether a string contains another string. I use the following code:
{% assign external_link = link.href contains '://' %}
For this snippet the external_link's value will be the same as the link.href's value (I checked the value of the external_link with the command {{ external_link }}).
I will get the same result even if I put parenthesizes around the right side:
{% assign external_link = (link.href contains '://') %}
What is the problem, and how can I get a true/false result of the contains expression?
You can use the capture filter tag to get the result of the contains tag:
{% assign link = "http://example.com "%}
{% capture has_link %}{% if link contains '://' %}Yes{% else %}No{% endif %}{% endcapture%}
{{has_link}}
{% assign link = "example.com "%}
{% capture has_link %}{% if link contains '://' %}Yes{% else %}No{% endif %}{% endcapture%}
{{has_link}}
Another option without capture
{% assign link = "http://example.com "%}
{% if link contains '://' %}
{% assign has_link = "yes" %}
{% else %}
{% assign has_link = "no" %}
{% endif %}
{{has_link}}
{% assign link = "example.com "%}
{% if link contains '://' %}
{% assign has_link = "yes" %}
{% else %}
{% assign has_link = "no" %}
{% endif %}
{{has_link}}
Output:
yes
no