Jinja2 template return nothing if variable is undefined - jinja2

I'm using Quickbase Pipelines trying to get some information to show in a notification email only if that variable (call rating) is defined. I've tried:
Audio Time: {{c.audio_time_min|int}}
{% if c.call_rating != '' %}
Call Rating: {{c.call_rating}}
{% else %}
''
{% endif %}
and
Audio Time: {{c.audio_time_min|int}}
{% if c.call_rating is defined %}
Call Rating: {{c.call_rating}}
{% else %}
''
{% endif %}
But I keep getting this as the response when that call rating variable is blank:
Audio Time: 21
Call Rating:
What I want is to not include that line at all, so:
Audio Time: 21

I think this might work:
Audio Time: {{c.audio_time_min|int}}
{% if c.call_rating not none %}
Call Rating: {{c.call_rating}}
{% endif %}

Quickbase Pipelines didn't let me save it with the "not none" test. What did work was:
Audio Time: {{c.audio_time_min|int}}
{% if c.call_rating %}
Call Rating: {{c.call_rating}}
{% endif %}
The result there if the Call Rating field is blank is just:
Audio Time: 23

Related

How to add a new property to Jekylls posts?

I want to do something like:
{% for post in site.posts %}
{% assign post.myProprty = "something" %}
{% endfor %}
But it doesn't work. Is that another way to do it?
I could make it happen using a plugin.
plugin code:
module Jekyll
module AddProp
def addProp(input, key, value)
input[key] = value
return input
end
end
end
Liquid::Template.register_filter(Jekyll::AddProp)
Usage:
{% for post in site.posts %}
{% assign post = post | addProp: "myProprty", "something" %}
{% endfor %}
Plugin GitHub repository

For loop in Shopify not working over number 9

I'm having an issue with a for loop in Shopify. I'm sure it used to work, but I can't get it to work over the number 9 now.
{% assign productTag1 = Availability14 %} (in this example, the product has only 1 tag, which is Availability14)
{% assign avail_stop = false %}
{% for j in (0..15) %}
{% assign check_avail = 'Availability' | append:j %}
{% if productTag1 contains check_avail %}
{% assign avail_stop = true %}
{% capture tag_name %}{{check_avail}}{% endcapture %}
{% break %}
{% endif %}
{% endfor %}
{% if avail_stop %}
{% assign availability = check_avail | remove:'Availability' | plus:0 %}
{% endif %}
At the moment, I'm returning 1, not 14. I imagine it's something to do with the fact 14 includes a 1, but I can't wrap my head around it.
Any help is appreciated.
You have a {% break %} statement in your if. Once the if becomes true it will exit the loop instantly.
If you want to skip the next code you must use {% continue %} not {% break %}.
On my mind this is an issue with conditional operator. As you said, 14 contains 1, so why not simply use strict conditional operator like this:
{% if productTag1 == check_avail %}
{% assign has_stop = true %}
{% break %}
{% endif %}
(or did I miss something?)

How do I display a message in Jinja (flask) if there is no object data to display

I have an object in python views.py that references the events database table. When there is data, it displays the data in my html template, however, when there is no data, I cannot figure out the {% if %} function that would display the message "No data found."
I have tried Tadeck's post, but the is defined always seems to evaluate to true even if there is no data to display. Thank you for your help.
{% if events is defined %}
value of variable: {{ events }}
{% else %}
variable is not defined
{% endif %}
views.py
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id)
return render_template(
self.template_file, events=events, the_event=the_event,
event_id=event_id
)
You are passing events as the query. You want it to be the query results:
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id).all()
Things to try:
{% if events %}
{% if events|length > 0 %}
{% if events != [] %}
This will help you. When event have data then it will go inside the if condition otherwise else will be execute No data found.
{% if events %}
value of variable: {{ events }}
{% else %}
No data found.
{% endif %}

Checking grains out of list with saltstack

I want to check if a specific version of a program is already installed.
Therefore, I got a state file:
{% set rvs = ['1113','1278'] %}
{% for rv in rvs %}
{% if ('r{{ rv }}' not in grains.get('cat12', [])) %}
... install it ...
{% else %}
... do nothing ...
{% endif %}
{% endfor %}
In my grains I have:
cat12:
- r1113
I would expect that '1278' is installed and list item '1113' triggers nothing, but even that is installed again...
There's no such syntax as using {{ and }} inside {% and %}. What's inside {% and %} is already Jinja. Just concatenate the string literal and the string variable with the ~ operator.
{% if 'r' ~ rv not in grains.get('cat12', []) %}
Or you can use the format filter:
{% if 'r%s'|format(rv) not in grains.get('cat12', []) %}

Jekyll Liquid Errors are puzzling me

I have been trying to create a site with Github and I'm new to the whole thing. I have been struggling with a piece of code that has been giving me some headache:
{% for item in site.static_files %}
{% if item.path contains '/archive' %}
{% if item.path contains 'index.html' == false %}
{% assign split_path = item.path | split: '/' %}
<p>{{item.path}}</p>
{% assign filename = split_path.last %}
<p>{{ filename }}</p>
{% endif %}
{% endif %}
{% endfor %}
This generates the following error:
Error:
Liquid Warning: Liquid Syntax Error (line 5): Expected end_of_string but found comparison in "item.path contains 'index.html' == false" in archive/index.html
Can anyone help?
Replace:
if item.path contains 'index.html' == false
With:
unless item.path contains 'index.html'
Liquid's getting confused.