Django comparing form field value with selected value - django-templates

I have a number of select box form fields which I need to output in the template with custom markup as a list. To do this I have created an include which gets passed the current form field as an argument.
I can loop over the list of options to create the list I want without any problems. My problem is that above the list of options I need to display either the form label, or the current selected value. This is the code I have to do this:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
If for example value in the above code is 1, and field.value is also 1, the equality test doesn't work and the bit of code within the if statement does not get executed. I guessed it might be down to the data type of the two values I was comparing, so I tried casting both to an integer as follows:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|add:"0" == field.value|add:"0" %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
This worked fine for the case where the values in the select box were integers. However, sometimes my select boxes have booleans as the value, so in this case the comparison won't work with the integer casting applied.
How can I safely compare the two values whatever the type of data is being compared?

I think I have found the answer.
The issue is that the selected value is always a string, and the value in the forloop could be an integer or a boolean (or potentially other data types). The answer is to cast the value in the forloop to a string:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|stringformat:"s" == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}

Related

How can I show Shopify variants depending on option value

How could you display only certain variants on the product and collection pages in Shopify depending on the value of an option?
Assuming the option name is 'Box Options' and the values would either be 'Trade' or 'Retail' I would expect something similar to this code to return the price for the Trade variant:
{%- assign get_variant = product.options_with_values -%}
{% assign trade_variant = get_variant | where: "Trade" %}
<p>{{ trade_variant.price }}</p>
For context I'm trying to get trade prices to be used where the customer tag is "Trade" using this if statement in the liquid file:
{% if customer.tags contains 'Trade' %}
Feel like this should be really easy but for the life of me I cannot get this to work.
If your variants contain only one option, then something like that should work (not tested):
{% if customer.tags contains 'Trade' %}
{% assign p_variants = product.variants | where:'title','Trade' %}
{% for variant in p_variants %}
{{ variant.title }}
{% endfor %}
{% endif %}
Then, second case, your variants go with multiple options, then it is a little bit more complex (not tested either):
{% if customer.tags contains 'Trade' %}
{% for variant in product.variants %}
{% if variant.options contains 'Box Options' and variant.title contains 'Trade' %}
{{ variant.title }}
{% endif %}
{% endfor %}
{% endif %}

Django is there an 'empty' with 'for' loop having a 'if' condition?

I have this:
{% for prop in props %}
{% if prop.status == 'SOLD' %}
{{ name }}
{% endif %}
{% empty %}
<li>No closed deals.</li>
{% endfor %}
and of course it doesn't do what I want, as far as an empty if loop.
Aside from just filtering the statuses into lists from the view, is there an empty for this kind of loop? I tried setting a true/false using a {% with sold=True %}, but it doesn't work that way.
You can do something like below, which would mean if a value is empty, False, or None it will evaluate to true:
{% if not prop.status %}
do something
{% endif %}
OR
use an else statement, which would catch all values that are not 'SOLD'
{% if prop.status == 'SOLD' %}
do stuff
{% else %}
do something else
{% endif %}

Use a Twig variable as a part of an other

I would like to use a Twig value comes from a "for" loop as a key in another for loop.
I have 2 arrays: "tab" dans "stats".
"tab" array has multiple values, with one named "name", which is an other array with keys and values.
I would like to use the "tab.name" value as a loop variable for my stats array.
I tried with no success to "add" the value directly in my variable.
{% for elem in tab %}
{% for data in stats.elem.name %}
------ My code
{% endfor %}
{% endfor %}
With this I don't have any result because Twig search for "stats.elem.name" array, but in fact for example if in a loop elem.name = "intitule", the real array is stats.intitule.
I hope i'm clear enough… :)
Thanks!
this should work
{% for elem in tab %}
{% for data in stats[elem] %}
{{data}}
{% endfor %}
{% endfor %}
https://twigfiddle.com/mzpjh8
You can do this by changing the array reference for stats to the standard square brackets - stats[elem.name]
{% for elem in tab %}
{% for data in stats[elem.name] %}
<!-- code -->
{% endfor %}
{% endfor %}

Is it possible to add more values in a for in loop Liquid Shopify?

Here's my code
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
There are some values I want to add to the filter_prefixes, example: Type...
Can I do it like this?
{% for filter_prefix in filter_prefixes %}
{% filter_prefix = 'Type' %}
{{ filter_prefix }}
{% endfor %}
To list all the filter_prefixes and added the Type value as well.
Please help me with this.
Thank you!
Just output the value you want. There is no need to add it to the loop since it is just something you output anyway.
Type
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
Or use the Shopify Array filters split and join. So you join your existing array filter_prefixes into a string, append Type and then split that back into an array.

How to append a variable inside another vaiable name in liquid html

Here is the code:
{% for i in (0..5) %}
{% assign product = recommendations.top_related_products_{{i}}.products[0].title %}
{{ product }}
{% endfor %}
This code throws the error Liquid error: Cannot read property '0' of null
I have 6 arrays under recommendation like top_related_products_0...5.
How do I print the title variable which is inside the top_related_products_0..5?
Create a string containing the variable name, then use square bracket notation to access it.
For example:
{% for i in (0..5) %}
{% capture related_products %}top_related_products_{{i}}{% endcapture %}
{{ recommendations[related_products].products.first.title }}
{% endfor %}