Search code examples
shopifytemplate-engineliquid

Liquid filter access itself within pipe chain


This works:

{% assign getWord = "hello world" | split: " " %}
{% assign getWord = getWord[0] %}
{{ getWord }}

I was just wondering if the same thing could be done in one line something like this:

{% assign getWord = "hello world" | split: " " | this[0] %}
{% assign getWord = "hello world" | split: " " [0] %}
{% assign getWord = "hello world" | split: " " | [0] %}
{% assign getWord = "hello world" | split: " " | getWord[0] %}

Solution

  • This will solve your exact problem where you need to access index 0 and the last index.

    {% assign getWord1 = "hello world" | split: " " | first %}
    <!-- Test 1: {{ getWord1 }} == hello -->
    {% assign getWord2 = "hello world" | split: " " | last %}
    <!-- Test 2: {{ getWord2 }} == world -->
    {% assign getWord3 = "hello world" | first %}
    <!-- Test 3: {{ getWord3 }} == h -->