{"id":111388,"date":"2019-04-05T17:00:00","date_gmt":"2019-04-05T17:00:00","guid":{"rendered":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/"},"modified":"2025-02-25T03:42:34","modified_gmt":"2025-02-25T03:42:34","slug":"liquid-operators-logical-and-comparison-operators","status":"publish","type":"post","link":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/","title":{"rendered":"Liquid Operators logical and comparison operators"},"content":{"rendered":"<p>Liquid is a widely recognized open-source project on GitHub and is used by many software projects, including content management systems and flat file site generators. Shopify uses Liquid as the foundation for its themes, enabling dynamic content on online store pages through tags, objects, and filters. However, Liquid has intentional limitations, such as the lack of a state concept, which restricts deep access to the platform\u2019s internals.<br \/>\nAlthough these limitations might seem restrictive to experienced programmers, they are carefully designed. <a href=\"https:\/\/avada.io\/blog\/liquid\" rel=\"noopener\" target=\"_blank\"><b>Liquid<\/b><\/a> operates based on five main elements: handles, operators, types, truthy and falsy values, and whitespace control. This article will delve into one of these basics\u2014Liquid operators, focusing specifically on logical and comparison operators.<\/p>\n<h2 id=\"liquid-operators-logical-and-comparison-operators\">Liquid Operators: logical and comparison operators<\/h2>\n<ul>\n<li><a href=\"#1\">Logic<\/a><\/li>\n<li><a href=\"#2\">Essential operators<\/a><\/li>\n<li><a href=\"#3\">Contains<\/a><\/li>\n<li><a href=\"#4\">Arrangement of operators<\/a><\/li>\n<\/ul>\n<p>Many logical and comparison operators has been accessed by liquid. Obviously, you can use operators to make up logic with control flow tags.<\/p>\n<h3 id=\"logic-1\">Logic {#1}<\/h3>\n<p>We can control the output to our template using a simple <code class=\"language-plaintext highlighter-rouge\">if<\/code>, or <code class=\"language-plaintext highlighter-rouge\">endif<\/code> statement. In many ways if statements are like questions.<br \/>\nA different piece of markup will be output depend on the answer to the question. There will be no mark up at all in some cases.<br \/>\nAnother way of understanding the logic is that it enable us to control the flow of a template and ultimately make decisions on which data is shown. It is worth noting that unlike output tags the inclusion of logic tags in your templates does not result in anything being directly rendered, in addition,  they enable us to control what is rendered exactly.<\/p>\n<p>In Shopify theme development, you will find yourself using if statements a lot, below is an example of logic liquid:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png\" alt=\"image\" \/><\/p>\n<p>This code illustrates how you can either show the number of items in a visitors cart or extract a link to your products.<\/p>\n<h3 id=\"essential-operators-2\">Essential operators {#2}<\/h3>\n<p>There are 8 essential operators. The symbol and meaning of these operators are listed in table below:<\/p>\n<table>\n<thead>\n<tr>\n<th>Symbol<\/th>\n<th style=\"text-align: center\">Meaning<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>==<\/td>\n<td style=\"text-align: center\">equals<\/td>\n<\/tr>\n<tr>\n<td>!=<\/td>\n<td style=\"text-align: center\">does not equal<\/td>\n<\/tr>\n<tr>\n<td>&gt;<\/td>\n<td style=\"text-align: center\">greater than<\/td>\n<\/tr>\n<tr>\n<td>&lt;<\/td>\n<td style=\"text-align: center\">less than<\/td>\n<\/tr>\n<tr>\n<td>&gt;=<\/td>\n<td style=\"text-align: center\">greater than or equal to<\/td>\n<\/tr>\n<tr>\n<td>&lt;=<\/td>\n<td style=\"text-align: center\">less than or equal to<\/td>\n<\/tr>\n<tr>\n<td>or<\/td>\n<td style=\"text-align: center\">logical or<\/td>\n<\/tr>\n<tr>\n<td>and<\/td>\n<td style=\"text-align: center\">logical and<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Below is an example of the equal operator:<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>\n{% if customer.has_account == true %}\n  Welcome back to our store!\n{% endif %}\n\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>You can combine many comparisons in a tag using the and and or operators, below is an example of combining two equal operators:<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>\n{% if product.type == \"Shirt\" or product.type == \"Shoes\" %}\n  This is a shirt or a shoe.\n{% endif %}\n\n<\/code><\/pre>\n<\/div>\n<\/div>\n<h3 id=\"contains-3\">Contains {#3}<\/h3>\n<p><code class=\"language-plaintext highlighter-rouge\">contains<\/code> test out for the existence of a substring inside a string.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>\n{% if product.title contains 'Pack' %}\n  This product's title contains the word Pack.\n{% endif %}\n\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>It also  test out for the existence of a string in an array of strings.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>\n{% if product.tags contains 'Hello' %}\n  This product has been tagged with 'Hello'.\n{% endif %}\n\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>Finally, <code class=\"language-plaintext highlighter-rouge\">contains<\/code> can only search strings. You are unable to use it to test out for an object in an array of objects.<\/p>\n<h3 id=\"arrangement-of-operators-4\">Arrangement of operators {#4}<\/h3>\n<p>In tags with more than one <code class=\"language-plaintext highlighter-rouge\">and<\/code> or <code class=\"language-plaintext highlighter-rouge\">or<\/code> operator, operators are tested in order from right to left.<br \/>\nYou are unable to change the order of operations using parentheses\u200a\u2014\u200aparentheses are invalid characters in Liquid and will stop your tags from working.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>We hope that this article will give you full information about the basic liquid in shopify and will be the foundation for you to practice other function in building your business page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Liquid is a widely recognized open-source project on GitHub and is used by many software projects, including content management systems and flat file site generators. Shopify uses Liquid as the foundation for its themes, enabling dynamic content on online store pages through tags, objects, and filters. However, Liquid has intentional limitations, such as the lack [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[356,383],"tags":[],"class_list":["post-111388","post","type-post","status-publish","format-standard","hentry","category-shopify","category-shopify-devdocs"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Liquid Operators logical and comparison operators in Shopify<\/title>\n<meta name=\"description\" content=\"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme&#039;s logic.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Liquid Operators logical and comparison operators in Shopify\" \/>\n<meta property=\"og:description\" content=\"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme&#039;s logic.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/\" \/>\n<meta property=\"og:site_name\" content=\"Avada Commerce\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/avada.io\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-05T17:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-25T03:42:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png\" \/>\n<meta name=\"author\" content=\"Sam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@avadaio\" \/>\n<meta name=\"twitter:site\" content=\"@avadaio\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Liquid Operators logical and comparison operators in Shopify","description":"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme's logic.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/","og_locale":"en_US","og_type":"article","og_title":"Liquid Operators logical and comparison operators in Shopify","og_description":"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme's logic.","og_url":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/","og_site_name":"Avada Commerce","article_publisher":"https:\/\/www.facebook.com\/avada.io\/","article_published_time":"2019-04-05T17:00:00+00:00","article_modified_time":"2025-02-25T03:42:34+00:00","og_image":[{"url":"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png","type":"","width":"","height":""}],"author":"Sam","twitter_card":"summary_large_image","twitter_creator":"@avadaio","twitter_site":"@avadaio","twitter_misc":{"Written by":"Sam","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#article","isPartOf":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/"},"author":{"name":"Sam","@id":"https:\/\/avada.io\/#\/schema\/person\/cc9c647e3ec94d00dc61ae2e653cafb7"},"headline":"Liquid Operators logical and comparison operators","datePublished":"2019-04-05T17:00:00+00:00","dateModified":"2025-02-25T03:42:34+00:00","mainEntityOfPage":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/"},"wordCount":516,"publisher":{"@id":"https:\/\/avada.io\/#organization"},"image":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png","articleSection":["Shopify","Shopify Devdocs"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/","url":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/","name":"Liquid Operators logical and comparison operators in Shopify","isPartOf":{"@id":"https:\/\/avada.io\/#website"},"primaryImageOfPage":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#primaryimage"},"image":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png","datePublished":"2019-04-05T17:00:00+00:00","dateModified":"2025-02-25T03:42:34+00:00","description":"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme's logic.","breadcrumb":{"@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#primaryimage","url":"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png","contentUrl":"https:\/\/cdn4.avada.io\/media\/shopify\/WKE8K6F.png"},{"@type":"BreadcrumbList","@id":"https:\/\/avada.io\/blog\/liquid-operators-logical-and-comparison-operators\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/avada.io\/"},{"@type":"ListItem","position":2,"name":"Shopify","item":"https:\/\/avada.io\/category\/shopify"},{"@type":"ListItem","position":3,"name":"Shopify Devdocs","item":"https:\/\/avada.io\/category\/shopify-devdocs\/"},{"@type":"ListItem","position":4,"name":"Liquid Operators logical and comparison operators"}]},{"@type":"WebSite","@id":"https:\/\/avada.io\/#website","url":"https:\/\/avada.io\/","name":"Avada Commerce","description":"Blog","publisher":{"@id":"https:\/\/avada.io\/#organization"},"alternateName":"Avada Commerce","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/avada.io\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/avada.io\/#organization","name":"Avada Commerce","alternateName":"Avada Commerce","url":"https:\/\/avada.io\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/avada.io\/#\/schema\/logo\/image\/","url":"https:\/\/avada.io\/wp-content\/uploads\/2023\/06\/Avada-logo.jpeg","contentUrl":"https:\/\/avada.io\/wp-content\/uploads\/2023\/06\/Avada-logo.jpeg","width":1200,"height":1200,"caption":"Avada Commerce"},"image":{"@id":"https:\/\/avada.io\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/avada.io\/","https:\/\/x.com\/avadaio","https:\/\/www.youtube.com\/@avadaio","https:\/\/www.linkedin.com\/company\/avadacommerce"]},{"@type":"Person","@id":"https:\/\/avada.io\/#\/schema\/person\/cc9c647e3ec94d00dc61ae2e653cafb7","name":"Sam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/fdce01bbdb8b8eaa162f8ed372062707058abcd85e8b233af7cb4e01191c75f4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/fdce01bbdb8b8eaa162f8ed372062707058abcd85e8b233af7cb4e01191c75f4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/fdce01bbdb8b8eaa162f8ed372062707058abcd85e8b233af7cb4e01191c75f4?s=96&d=mm&r=g","caption":"Sam"},"description":"Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Vietnam. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.","sameAs":["https:\/\/wordpress-466542-3512282.cloudwaysapps.com","https:\/\/vn.linkedin.com\/in\/samng80"],"url":"https:\/\/avada.io\/authors\/sam\/"}]}},"meta_title":"Liquid Operators logical and comparison operators in Shopify","meta_description":"This guide explores Liquid operators, including logical and comparison operators, empowering you to create dynamic content and control the flow of your Shopify theme's logic.","meta_keywords":"","feature_image_custom":"","updated_at_migrate":"2024-08-29T21:43:02+07:00","_links":{"self":[{"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/posts\/111388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/comments?post=111388"}],"version-history":[{"count":1,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/posts\/111388\/revisions"}],"predecessor-version":[{"id":118002,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/posts\/111388\/revisions\/118002"}],"wp:attachment":[{"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/media?parent=111388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/categories?post=111388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avada.io\/wp-json\/wp\/v2\/tags?post=111388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}