Removing Single Quotes from a String with Liquid

Working with strings using Liquid can be a very powerful tool within Looker, but it also can add another complexity layer when you use it, as this is an external template language created by Shopify.

One of the weird cases I came across was when I had to extract values from a string list similar to this, without using SQL:

['cat', 'dog', 'bird', 'insect']

To create an HTML list as described here: Using Liquid Split and Array Filters to Format Lists
 

Like this:

  • cat
  • dog
  • bird
  • insect

The problem was that using the single quote as is, or escaped, was not removing the single quotes ' around the strings.
 

Test 1:

html:

  {% assign animals = value | remove: "'" | remove: "[" | remove: "]" | split: ", " %}

  <ul>

  {% for animal in animals %}

    <li>{{ animal }}</li>

  {% endfor %} ;;

Test 2:

html:

  {% assign animals = value | remove: "\'" | remove: "[" | remove: "]" | split: ", " %}

  <ul>

  {% for animal in animals %}

    <li>{{ animal }}</li>

  {% endfor %} ;;

Result (for both tests):

  • 'cat'
  • 'dog'
  • 'bird'
  • 'insect'

In order for this to work, the HTML Code for the single quote “&#39;” can be used, like this:

html:

  {% assign animals = value | remove: "&#39;" | remove: "[" | remove: "]" | split: ", " %}

  <ul>

  {% for animal in animals %}

    <li>{{ animal }}</li>

  {% endfor %} ;;

Result:

  • cat
  • dog
  • bird
  • insect
2 0 2,839
0 REPLIES 0
Top Labels in this Space
Top Solution Authors