Simonski
Simonski

Reputation: 121

Liquid: check if object contains certain string

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?

Upvotes: 3

Views: 14945

Answers (1)

koosa
koosa

Reputation: 3040

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 %}">

Upvotes: 6

Related Questions