1

I want to split my variable into two new variables before and after the dash character "-".

How do I adjust my code below to enable this to happen?

{% set name = 'first-last' %}
{% set firstNameSlug = name|split('-')[1] %}
{% set lastNameSlug = name|split('-')[2] %}

{{ firstNameSlug }}
{{ lastNameSlug }}
2

If I'm following you, arrays are 0-indexed, so this would work (with a bit of cleanup).

{% set names = 'first-last'|split('-') %}
{% set firstNameSlug = names[0] %}
{% set lastNameSlug = names[1] %}

{{ firstNameSlug }}
{{ lastNameSlug }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.