How can I simplify this lengthy conditional in my code?

I’m looking to DRY this up.

  • Performing a for loop and organizing all collections A-Z while setting the current section with product_vendor | slice: 0 works fine.
  • However, if a collection name starts with a number, then I’m not listing each as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 - but assigning “0-9” as the section’s title.

The conditional is way too long-winded and would like to know how to refactor it.

{% if thisVendorDir == "0" or thisVendorDir == "1" or thisVendorDir == "2" or thisVendorDir == "3" or thisVendorDir == "4" or thisVendorDir == "5" or thisVendorDir == "6" or thisVendorDir == "7" or thisVendorDir == "8" or thisVendorDir == "9" %}
    {% assign thisVendorDir = "0-9" %}
{% endif %}

So you have…

thisVendorDir

…already set as a var being the first char in the title.

So I would just do this:

{% if "1234567890" contains thisVendorDir %}
  {% assign thisVendorDir = "0-9" %}
{% endif %}
1 Like

@Jason - Awesome! I appreciate the assistance.

Using the cheat sheet is often helpful, however, sometimes the syntax collides with other languages.
https://www.shopify.com/partners/shopify-cheat-sheet

For instance, contains seems like you’d be defining the variable first and what it could contain. Not the other way around.

Thanks for clearing that up for me.