If Statement depending on the Category a Product belongs to (Big Cartel) - bigcartel

Can I create an if statement on the Product.html page depending on the category which a product belongs to? Something like this:
{% if product.category.name == 'Shirts' %}
<div>Some Content</div>
{% endif %}
Thanks.

Since a product can be listed in multiple categories, you'll need to use a for loop to go through all the categories, and then include an if statement to check the name - like so:
{% for category in product.categories %}
{% if category.name == 'Shirts' %}
<div>Your content</div>
{% endif %}
{% endfor %}

Related

Flaks / Jinja nested if statement

I am stuck on a nested if statement, thinking I am approaching it wrong. I have a list of about 100 products that I am rendering on my page. I want to have a nested 'if' statement. Users will have a toolkit, and if the product is in their toolkit, i want to render "Already Uses", else "Add to toolkit?"
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% for products in toolkit %}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
more details on: jinja2 check if value exists in list of dictionaries
You are spot on and on the right path. It would help to provide the structure (as an example) of the product object ...
{% for product in products|sort(attribute="name") %}
{{ product.name }}
{% if product.name in toolkit %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}
{% endfor %}
If toolkit is a nested map map:
{% if product.name in toolkit|map(attribute="<whatever has the value equal with product.name>") %}
<p>Already Uses<p>
{% else %}
<p>Add to toolkit?<p>
{% endif %}

in django template unable to show content inside if block

when i write if statement nothing show in html but when i remove the if statement the code show all items what is wrong
{% for catagory in catagory_list %}
{% for item in product_list %}
{% if item.catagory == "Fruits" %}
<p>{{item.catagory}}</p>
<p>{{item.name}}</p>
<p>{{item.price}}</p>
<img src="{{item.image.url}}" alt="">
{% endif %}
{% endfor %}
{% endfor %}
Because catagory is an Object, Try:
{% if item.catagory.name == "Fruits" %}
assuming that catagory model has an attribute "name"

If statement in my Django template...Is there a better way?

In my Django template: I'm trying to add an extra div around my for loop only if the length of the data being passed to the template is 3. This is what I'm trying right now but it seems like there could be better way than doing two if statements to check for the length:
{% if items|length == 3 %}
<div class='three-item-wrap'>
{% endif %}
{% for item in items %}
.......
{% endfor %}
{% if items|length == 3 %}
</div> //close .three-item-wrap
{% endif %}
you can try like that
{% if items|length == 3 %}
<div class='three-item-wrap'>
{% for item in items %}
.......
{% endfor %}
</div>
{% else %}
#another logic goes here
{% endif %}
if you want know more refer the docs django if tempalate
I think better way would be to make single if statement check. Just like this:
{% if items|length == 3 %}
<div class='three-item-wrap'>
{% for item in items %}
.......
{% endfor %}
</div>
{% else %}
{% for item in items %}
.......
{% endfor %}
{% endif %}
This way is better because of Django render engine, which firstly check if statements and then do for loop.
And if something crash in your code, div will be without closing tag </div>. Instead in my code there is no option for div to be without closing tag.

Breaking out of a for loop from a nested if statement in Django

is there a way to break out of this for loop from inside the if statement. Currently our database is incorrectly storing multiple primary phones and I would like to break out of the for loop after the first primary phone is found. Thank you in advance for any help.
{% for phone in user_phones %}
{% if phone.primary %}
<div>{% if phone.type %}{{ phone.type|title }}: {% endif %}<span itemprop="telephone">{{ phone.phone_format }}</span></div>
{% endif %}
{% endfor %}
Updated:
Or just fail the if condition by creating a variable within the if true branch
If you have to stay within the template layer you could use regroup.
{% regroup user_phones|dictsort:"primary" by primary as phones_list %}
{% for phone in phones_list %}
{% if phone.grouper %}
{{ phone.list.0.type }}
{% endif %}
{% endfor %}
What it does
regroup together with the dictsort filter (which also works on querysets) groups the instances in user_phones by the value of primary.
regroup will add an attribute named grouper, which when grouping by a bool (the value of primary) will either be True or False.
for then iterates over the variable phones_list, which is provided by regroup. Since we have sorted the results by primary, {% if phone.grouper %} will tell us when we hit the group of items with primary == True.
regroup packs the items that belong to a group into the attribute list. So the first item can be accessed with phone.list.0.type, phone.list.0.phone_format, etc.
Note:
if you need to access foo.list.0 many times it can be assigned to a variable (using with):
{% regroup user_phones|dictsort:"primary" by primary as phones_list %}
{% for items in phones_list %}
{% if items.grouper %}
{% with items.list.0 as phone %}
<div>{% if phone.type %}{{ phone.type|title }}: {% endif %}<span itemprop="telephone">{{ phone.phone_format }}</span></div>
{% endwith %}
{% endif %}
{% endfor %}
There is no break in Django templates. You may handle it in your view by storing the primary phone that you are looking for to a variable and then calling it in your template.

Django {% with %} tags within {% if %} {% else %} tags?

So I want to do something like follows:
{% if age > 18 %}
{% with patient as p %}
{% else %}
{% with patient.parent as p %}
...
{% endwith %}
{% endif %}
But Django is telling me that I need another {% endwith %} tag. Is there any way to rearrange the withs to make this work, or is the syntactic analyzer purposefully carefree in regards to this sort of thing?
Maybe I'm going about this the wrong way. Is there some sort of best practice when it comes to something like this?
if you want to stay DRY, use an include.
{% if foo %}
{% with a as b %}
{% include "snipet.html" %}
{% endwith %}
{% else %}
{% with bar as b %}
{% include "snipet.html" %}
{% endwith %}
{% endif %}
or, even better would be to write a method on the model that encapsulates the core logic:
def Patient(models.Model):
....
def get_legally_responsible_party(self):
if self.age > 18:
return self
else:
return self.parent
Then in the template:
{% with patient.get_legally_responsible_party as p %}
Do html stuff
{% endwith %}
Then in the future, if the logic for who is legally responsible changes you have a single place to change the logic -- far more DRY than having to change if statements in a dozen templates.
Like this:
{% if age > 18 %}
{% with patient as p %}
<my html here>
{% endwith %}
{% else %}
{% with patient.parent as p %}
<my html here>
{% endwith %}
{% endif %}
If the html is too big and you don't want to repeat it, then the logic would better be placed in the view. You set this variable and pass it to the template's context:
p = (age > 18 && patient) or patient.parent
and then just use {{ p }} in the template.