Shopify-code-snippets
Shopify-code-snippets copied to clipboard
starts-with example doesn't give expected result
Using the starts-with example, I tried to detect if the current page URL matches a certain page:
{% assign stringToCheck = page.url %}
{% assign startsWith = '/about/' %}
{% assign checkArray = stringToCheck | split:startsWith %}
{% if checkArray[0] == blank %}
The string {{ stringToCheck }} does start with {{ startsWith }}.
{% else %}
The string {{ stringToCheck }} does NOT start with {{ startsWith }}.
{% endif %}
However, with that snippet, I get this on the page:
The string /about/ does NOT start with /about/.
Though, this works for me:
{% assign stringToCheck = page.url %}
{% assign startsWith = '/about/' %}
{% assign checkArray = stringToCheck | split:startsWith %}
{% assign checkSlice = stringToCheck | slice: 0, stringToCheck.size %}
{% if checkSlice == startsWith %}
The string {{ stringToCheck }} does start with {{ startsWith }}.
{% else %}
The string {{ stringToCheck }} does NOT start with {{ startsWith }}.
{% endif %}
Which, as I implied, outputs:
The string /about/ does start with /about/.