I am trying to make a multiple condition statement on Big Cartel and I keep getting an error telling me that there is an unknown tag in my When statement. I feel like this is very simple. What am I doing wrong? Thank you for looking.
{% case product.status %}
{% when 'sold-out' %}
{% if product.id = '25027747' %}
RESULT 1
{% else %}
{% if product.id = '25027993' %}
RESULT 2
{% else %}
{% endif %}
You'll want to use this code:
{% case product.status %}
{% when 'sold-out' %}
{% if product.id = '25027747' %}
RESULT 1
{% elsif product.id = '25027993' %}
RESULT 2
{% endif %}
{% endcase %}
Related
I want to define a variable in my Django template but if I set the variable within the statement I can't call it again. Is there any way to call the variable from the other part of my template? Thank you in advance!
My attemt:
{% if a > 1 %}
{% with "string_1" as var %}
{% endwith %}
{% elif a < 1%}
{% with "string_2" as var %}
{% endwith %}
{% else %}
{% with "string_3" as var %}
{% endwith %}
{% endif %}
{% if var='string_1' %} #here i want to call the variable again
<p>This is string_1</p>
{%else%}
<p>This is not string_1</p>
I'm hoping for some help with liquid in Shopify, I'm having trouble with trying to customise the cart based on what product the customer has added.
Basically if a customer adds product from Vendor A then I want the cart to load the template to customise the cart for Vendor A
But if product is from Vendor B then I want it to load the template to customise the cart for Vendor B
But if the cart has products from neither (or both) then I want it to load the default cart.
{Edit: I worked out what was wrong with my code to load the templates but now I just need help with the logic so that when the cart has products from both brands it loads the default cart. Because at the moment it loads both cart snippets into the page}
Any help massively appreciated!
{% for item in cart.items %}
{% if item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% elsif item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% else %}
{% section 'cart-default %}
{% endif %}
{% endfor %}
also tried this:
{% case cart.items %}
{% when item.vendor == 'Brand A' %}
{% include 'cart-a' %}
{% when item.vendor == 'Brand B' %}
{% include 'cart-b' %}
{% when item.vendor == ‘Brand A’ and item.vendor == 'Brand B' %}
{% section 'cart-default' %}
{% else %}
{% section 'cart-default' %}
{% endcase %}
I think this steps may help you...
Step 1: Create different section instead of snippets for two different types of vendors and default
Step 2: Follow the below code in cart.liquid
{% assign vendor = '' %}
{% assign same = true %}
{% for item in cart.items %}
{% if vendor != '' or vendor == item.vendor %}
{% assign vendor = item.vendor %}
{% else%}
{% assign same = false %}
{% endif %}
{% endfor %}
{% if same == true %}
{% if vendor == 'Brand A' %}
{% section 'cart-a' %}
{% elsif vendor == 'Brand B'%}
{% section 'cart-b' %}
{% else %}
{% section 'cart-default' %}
{% endif %}
{% else %}
{% section 'cart-default' %}
{% endif %}
In liquid it is easier to work with arrays. Working code for you:
{% assign vendors = cart.items | map: 'vendor'| uniq | join: ' ' %}
{% if vendors contains "Brand A" and vendors contains "Brand B" %}
{% section 'cart-default' %}
{% else %}
{% if vendors contains "Brand A" %}
{% section 'cart-a' %}
{% else %}
{% if vendors contains "Brand B" %}
{% section 'cart-b' %}
{% else %}
{% section 'cart-default' %}
{% endif %}
{% endif %}
{% endif %}
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.
I have the following loop set up, but need to remove the comma on the last item (it's to replicate a JSON array for cycle2)
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>",
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
Cheers,
R
What about this?
{% for product_in_series in series.get_products %}{%spaceless%}
{% with product_in_series.product as product %}
{%if not forloop.first%}
"<img src='{% version product.get_overview 'page_image' %}'>"
{%if not forloop.last%},{%endif%}
{%endif%}
{% endwith %}
{%endspaceless%}{% endfor %}
{{ forloop.last|yesno:",,"|safe }}
, - is a comma
None of the above works for me.
The correct syntax, as in Django 3.0, is as such
{% with querythisandthat as A %}
{% for u in A %} {{ u.interesting_stuff }}
{% if u == A.last %} . {% else %} ; {% endif %}
{% endfor %}
{% endwith %}
The reason is that A.last is not True/False, but it is the last element of the queryset
https://docs.djangoproject.com/en/3.0/ref/models/querysets/#django.db.models.query.QuerySet.first
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.