Skip to content

starts-with example doesn't give expected result #6

@KyNorthstar

Description

@KyNorthstar

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/.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions