Is it possible to put multiple conditions inside a single if statement in Shopify Liquid?
For example this is what I have:
{% if product.type != "A" and product.type != "B" and product.type != "C" %}
//do something
{% endif %}
The reason I'm wondering is because syntax highlighting stops after the first "and" operator, as if everything after that has incorrect syntax.
Your syntax is 100% valid and will work in Shopify
{% if product.type != "A" and product.type != "B" and product.type != "C" %}
//do something
{% endif %}
I guess the editor what you are using has a bug in syntax highlighter for liquid. That's why you confused
Related
So on my front-end, I want to show some HTML only if the user belongs to one of 2 groups: 'admins' or 'clerks'. There are 3 groups of users: 'admins', 'clerks', and 'sellers'. Here is my front-end code:
{% if user.groups.all.0 == "admins" %}
<h1>Some HTML</h1>
{% elif user.groups.all.0 == "clerks" %}
<h1>Some HTML</h1>
{% endif %}
When I run this code, the HTML shows for the admins. But that of the clerks does not show. I have tried printing out the group to be sure that the spelling and the casing were the same, and they were the same. And Django does not throw an error. It only works if I rewrite the code as follows:
{% if user.groups.all.0 == "admins" %}
<h1>Some HTML</h1>
{% elif user.groups.all.0 != "sellers" %}
<h1>Some HTML</h1>
{% endif %}
But I feel like this is not good design. Please am I missing something? Thank you all in advance
First of all, is a not practical idea for too many reasons.
Think that using that method you are getting possibilities and processing that don't correspond to the templates. The templates engine philosophy is just basic and no important logic.
A way to have more control and more practical is using a flag. For example
groups_permited_for_this=["admins","clerks",...]
Permited =False
for group in user.Groups.all:
if group.name in groups_permited_for_this:
Permited =True
Pass it as context and then use a Jinja if statement.
So I resolved the issue in a slightly different way from the one above:
First, I created a folder called 'templatetags' inside the app.
Then I created 2 files in it:
An empty 'init.py' file
A 'cust_auth.py'(for custom authentication) file
Inside the 'cust_auth.py' file I wrote a function that does a similar thing as in #Paulo Aguilar's answer above:
from django import template
from django.contrib.auth.models import Group
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
Then in my template, I loaded the 'cust_auth.py' file at the top:
{% load auth_extras %}
Then I did the logic I was looking for:
{% if user|has_group:"admins" %}
<h1>Some HTML</h1>
{% endif %}
I preferred this because I think over the cause of my application, I will want more custom authentications
Thank you Paulo and whoever posts
H I want to build a warning message with ansible that is stored in a variable:
I have {{total}} and {{wanted}} and I want to store that in a {{outcome}} variable :
I need something like :
if ({{total}} < {{wanted}}): {{outcome}}= "you need to much"
else if ({{total}} = {{wanted}}): {{outcome}}= "Could work out"
else: {{outcome}}= "Ok"
How can I get this to work?
Thanks for any ideas... this is driving me crazy....
The task below does the job
- set_fact:
outcome: |
{% if total|int < wanted|int %}
You need to much
{% elif total|int == wanted|int %}
Could work out
{% else %}
Ok
{% endif %}
I use the Order Printer app in Shopify to print my orders. I have edited the the template to suit my needs, however I am quiet new to Liquid code.
Based on the shipping postcode of the order, I need the template to return 1 of 3 labels - Rural, Major and Outer. I have a list of postcodes in the following format (this is a small portion for example):
Rural
2648, 2715, 2717-2719, 2731-2739, 3221-3334, 3342-3349, 3351-3352, 3357-3426, 3444-3688, 3691-3749, 3812-3909, 3921-3925, 3945-3974, 3979, 3984-3999
Major
1000-1935, 2000-2079, 2085-2107, 2109-2156, 2158, 2160-2172, 2174-2229, 2232-2249, 2557-2559, 2564-2567, 2740-2744, 2747-2751, 2759-2764, 2766-2774, 2776-2777, 2890-2897
Outer
7020-7049, 7054, 7109-7150, 7155-7171, 7173-7247, 7255-7257, 7330-7799
I'm unable to work out how to use the if statement for the purpose of identifying if the shipping postcode is a rural, major or outer postcode, without typing out every postcode between 7330 and 7799 etc.
Can anyone help?
First declare your arrays:
{% assign Rural= "2648, 2715, 2717-2719, 2731-2739, 3221-3334, ...." | split: ", " %}
{% assign Major= "1000-1935, 2000-2079, 2085-2107, 2109-2156,..." | split: ", " %}
{% assign Outer= "7020-7049, 7054, 7109-7150, 7155-7171,...." | split: ", " %}
Then declare a variable that you will use for the label
{% assign relatedLabel = ""%}
Implement the if logic
{% if Rural contains Order.PosteCode %}
{% assign relatedLabel = "rural" %}
{% endif %}
{% if Major contains Order.PosteCode %}
{% assign relatedLabel = "major" %}
{% endif %}
{% if Outer contains Order.PosteCode %}
{% assign relatedLabel = "outer" %}
{% endif %}
Finally you can print it where you need it
This Poste Code belongs to {{relatedLabel}} area.
Seems like elementary question, and yet can't get it work
{% if iterator.next > 10 %}
Do smth
{% endif %}
Two issues. First, this code just won't work (the code in the if-condition never implemented even when the condition seems to hold true), and second issue - the ">" sign is highlighted as if it where the closing tag of the closest open tag. Any ideas how to fix the first issue and is it all right with second issues ? Maybe there's some elegant syntax that I am missing and that would remove this ambiguity for the text editor ?
iterator.next may be a string which would result in the statement being False.
Try creating a custom filter to convert it to an int. For example create "my_filters.py":
# templatetags/my_filters.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
Then in your template:
{% load my_filters %}
{% if iterator.next|to_int > 10 %}
Do smth
{% endif %}
More on custom tags and filters here
I wouldn't worry about the highlighting, this may just be your IDE. I recommend using PyCharm for Django development
Django's docs says that you can use > with if tag:
{% if somevar < 100 %}
This appears if variable somevar is less than 100.
{% endif %}
take a look at documentation: https://docs.djangoproject.com/en/1.9/ref/templates/builtins/
maybe you are missing something else?
if...true conditionals work like a charm as outlined here in the docs.
but if I try to do something like:
{% if !posts.length %}
<i>No project posts yet!</i>
{% endif %}
I get an error:
Template render error: (/home/nak/clones/mf3/views/project.html) [Line 10, Column 9]
unexpected token: !
I've worked around this by doing:
{% if posts.length %}
{% else %}
<i>No project posts yet!</i>
{% endif %}
Is there a better (correct) way to do this?
I see you've got a bit of a bobby dazzler here.
Try using not instead of !.
In other words, use not, not !!
Give 'er a go mate and notice that in the raw section here they highlight not as if it's a keyword.
https://mozilla.github.io/nunjucks/templating.html#raw
Best of luck to ye.
You can use the syntax:
<% '' if posts.length else 'No project posts yet!' %>
https://mozilla.github.io/nunjucks/templating.html#if-expression