Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Consider the following problem:
{% assign some_id = product.id | string %}
Does not work. Why?
This should work fine in Liquid:
###
Lets move to an array with the CONTAINS operator
{% assign arr_prep = "1001,2222,3333,4567" %}
{% assign my_array = arr_prep | split: ',' %}
{% assign id_to_check = 4567 %}
{% if my_array contains id_to_check %}
this never displays
{% endif %}
Not work
I would think we should be able to use the CONTAINS operator in this way
and since we can not typecast using | string we have to do this...
###
Lets try with a typecast...
{% assign arr_prep = "1001,2222,3333,4567" %}
{% assign my_array = arr_prep | split: ',' %}
{% assign id_to_check = 4567 | string %}
{% if my_array contains id_to_check %}
this also never displays
{% endif %}
Also not work.
###
Lets do it in the worst way possible
{% assign arr_prep = "1001,2222,3333,4567" %}
{% assign my_array = arr_prep | split: ',' %}
{% assign id_to_check = 4567 | append: "" %}
{% if my_array contains id_to_check %}
now this will display because I "appended" id_to_check with a "" string
{% endif %}
This works.
But all the other examples are far more logical and expected as a good standard
###
Hi @cfc_webmaster There are some serious misunderstandings in these posts, mostly in over expectation of what liquid should be capable of doing.
Keep in mind liquid is intended to be simple, do not expect programmatic conveniences developers expect in a programming language to be inside a template language meant for merchants. There will not be stuff like type-checking or casting of primitive types.
If the point of these is to show some sort of failure on liquids part as some sort of feature request to shopify staff that will generally fall on deaf ears.
To get real change such as typecasting filters in liquid you need to go up the chain to the liquid template engine project itself.
https://github.com/Shopify/liquid
There is no string filter {{ | string }} in shopify's liquid.
Use Implicit typecast {{ 1234 | append:"" }} .
Liquid is not a universal standard , liquid comes in different "flavors" that each platform tailors to their needs.
What you read on one platforms blogs (Dynamics 365) may not translate to shopifys version of liquid.
If you do not see it in the liquid shopify.dev reference it should be treated as unsupported on the platform.
https://shopify.dev/api/liquid
In learning a language before getting to the string vs number comparisons reduce the problem space to discrete primitives then work up .
First check the behaviors of number vs number comparisons:
{% assign one = 1 %}
{% assign another_one = 1 %}
{% if one == another_one %}
one == first: true
{% endif %}
{% if one contains another_one %}
one contains first: true
{% endif %}
Contains works on strings, to work on numbers use boolean operators
https://shopify.dev/api/liquid/basics#operators
You can use contains to check for the presence of a string within an array, or another string. You can’t use contains to check for an object in an array of objects.
Then check the array string --> array creation behavior to see what happens to array items:
{% assign arr_prep = "1001,2222,3333,4567" %}
{% assign my_array = arr_prep | split: ',' %}
{% assign id_to_check = 4567 %}
{% for item in my_array %}
{% if item == id_to_check %}
{% assign match = true %}
{{ item }} matches {{ id_to_check }}: {{ match }}
{% else %}
{{ item }} does not match {{ id_to_check }}: {{ match }}
{% endif %}
{% endfor %}
In most every area of shopify themes where strings or number may intermix, or user entered data of strings will be compared you should normalize the things being compared.
For string to string comparisons this may involve removing excess newlines, downcases or trimming spaces on the end of the string,etc. https://shopify.dev/api/liquid/filters#string-filters
{%- for item in my_array -%}
{%- assign normalized_item = item | plus: 0 -%}
{%- assign normalized_id_to_check = id_to_check | plus: 0 -%}
{%- if normalized_item == normalized_id_to_check -%}
{%- assign match = true -%}
{{ item }} matches {{ id_to_check }}: {{ match }}
{%- else -%}
{{ item }} does not match {{ id_to_check }}: {{ match }}
{%- endif -%}
{%- endfor -%}
Contact paull.newton+shopifyforum@gmail.com for the solutions you need
Save time & money ,Ask Questions The Smart Way
Problem Solved? ✔Accept and Like solutions to help future merchants
Answers powered by coffee Thank Paul with a ☕ Coffee for more answers or donate to eff.org
In Canada, payment processors, like those that provide payment processing services t...
By Jacqui Mar 14, 2025Unlock the potential of marketing on your business growth with Shopify Academy's late...
By Shopify Mar 12, 2025Learn how to increase conversion rates in every stage of the customer journey by enroll...
By Shopify Mar 5, 2025