Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get substring from string in Liquid?

I am working with Jekyll and I have the string balh blah blah&garbage **&*&% garbage <h1>TITLE</h1> &^*$%"

Is there a way to grab TITLE? I have looked at the functions here but I don't see something that I can use.

like image 423
Pithikos Avatar asked Jan 08 '15 09:01

Pithikos


2 Answers

split to the rescue !

{% assign str = 'garbage <h1>TITLE</h1> moregarbage' %}
{% assign a = str | split: '<h1>' %}

We now have garbage in a[0] and TITLE</h1> moregarbage in a[1]

{% assign b = a[1] | split: '</h1>' %}

We now have TITLE in b[0] and moregarbage in b[1]

like image 182
David Jacquel Avatar answered Nov 09 '22 12:11

David Jacquel


I know this is ancient, but for anyone else coming across it: https://shopify.github.io/liquid/basics/operators/

contains contains checks for the presence of a substring inside a string.

{% if product.title contains "Pack" %} This product's title contains the word Pack. {% endif %}

contains can also check for the presence of a string in an array of strings.

{% if product.tags contains "Hello" %} This product has been tagged with "Hello". {% endif %}

contains can only search strings. You cannot use it to check for an object in an array of objects.

like image 21
Dave Avatar answered Nov 09 '22 11:11

Dave