How to use IF statement in twig for given use case - html

I am using twig for templating
{{ content.field_votre_type_the_profil[0] }} returns "test string"
so when i compare it in if statement it not working...
for example
{% if content.field_votre_type_the_profil[0] == "test string" %}
<p>Hey i found the string</p>
{% endif %}
I am not getting the "Hey i found the string" in output..
Any idea?

Try reversing the logic, and say:
{% if content.field_votre_type_the_profil[0] != "test string" %}
If your P tag appears, there is a problem with your value being lost/overwritten somewhere.
If you can't get it to print your html no matter what your logic is: check that your syntax is correct, make sure there are no errors happening with the plugin itself, etc.
Let me know if you've tried anything else, I'll respond with more help.
Try this and tell me what it prints:
{% if content.field_votre_type_the_profil[0] == "test string" %}
<p>Hey i found the string</p>
{% else %}
<h1>Your variable is being overwritten, or not included for some reason</h1>
{% endif %}
{{ content.field_votre_type_the_profil[0] }}

I was able to solve it..
I had to go one level deeper to do this if calculation.
{% if content.field_votre_type_the_profil[0]['#markup'] == "test string" %}

Related

How do I find if a word is found in a string in Liquid for Shopify?

Per the documentation for Liquid (https://shopify.github.io/liquid/basics/operators/) I can use the keyword contains to find out if a word is present in a String. However, when I do the following code, it breaks my code:
{% if item.metadata['shopify_tags'] contains "outlet" %}
<br>All items are final sale, non-refundable, and not eligible for exchange.
{% endif %}
When using Postman, the JSON returned is:
"metadata": {
"shopify_product_type": "Pajama Set",
"shopify_tags": "_tab_outlet, _tab_pajama-set-sizing, birthday, bride, cities, city, city toile, customshop, fashion, monogramshop, mother, Nashville, Nashville Toile, new, outlet, pajama sets, pj, pjs, retail, Toile, Toile Shop, womens",
"barcode": "901868449",
"shopify_current_inventory": "66",
"current_stock": "66",
"ShopifyVariantID": "32662090186849"
It seems that as soon as I use the word "contains", it breaks my code. How would I search if "outlet" is found in the product tags?
EDIT: Apparently I'm working in Twig instead of just Liquid. This works:
{% if "outlet" in item.metadata['shopfy_tags'] %}
...
{% endif %}
You should not be working with the JSON code when you have access to the backend liquid. There is nothing called item.metadata['shopify_tags]' in shopify It should be product.tags if you are in the product page.
Shopify Cheat Sheet
{% for tag in product.tags %}
{% if tag == "_tab_outlet" %}
do something
{% elsif tag contains "outlet" %}
do something
{% endif %}
{% endfor %}

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 %}`

How do I access a data value in a dictionary in the html template

I'm passing a dictionary in the context in my views.py to my html template. How do I know access a value in the template based on a particular key. For instance I'd wanna do something like {{ dictionary.keyname.value }} but I don't know the correct syntax and for some reason I can't seem to find the documentation.
I want to achieve the same effect as this without having to use a for loop:
<b>Calories</b>
{% for key, value in output.items %}
{% if key == "calories" %}
{{ value }}
{% endif %}
{% endfor %}
You just want {{ output.calories }}.

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)

Get data from data folder?

I need to get some data using a dynamic name from front matter:
{{ site.data.prd-[page.tag].title" }}
The above fails to get the string from the data folder and nothing is output.
Where am I going wrong?
I am not sure about what you are trying to do but how about make a dictionary type data in _data directory and search for a title that matches page.tag in desired pages?
_data.prd-tags.yml
For each item, we can access the key and value using index ([0]: key, [1]: value).
jekyll: "jekyll's title"
ruby: "ruby's title"
html: "html's title"
.
.
.
HTML fragment for searching and displaying a tag (if found)
Iterate over site.data.prd-tags and display title if an item that matches page.tag is found.
<div class="tag-title">
{% for tag in site.data.prd-tags %}
{% if tag[0] == page.tag %}
{{ tag[1] }}
{% else %}
Nothing found
{% endif %}
{% endfor %}
</div>