Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Liquid: check if object contains certain string

Tags:

shopify

liquid

In a shopify template, I create a link with

<a href="{{ featured.url | within: collection }}" class="grid-link-slider">

I need to check if the url contains a certain word "postcard", and if true, set a second class. Unfortunately,

<a href="{{ featured.url | within: collection }}" class="grid-link-slider {% if {{ featured.url | within: collection }} contains "postcard" %}grid-link-postcard{% endif %}">

is not working, "contains" can only be used for strings and also the syntax for sure is invalid. Is there a way to solve this?

like image 308
Simonski Avatar asked Jun 26 '26 07:06

Simonski


1 Answers

I would capture it like so:

{% capture featured_url %}{{ featured.url | within: collection }}{% endcapture %}
<a href="{{ featured_url }}" class="grid-link-slider {% if featured_url contains "postcard" %}grid-link-postcard{% endif %}">
like image 164
koosa Avatar answered Jun 27 '26 23:06

koosa