Hay and needle question (Liquid)

by Caroline Schnapp - Member - 07:27PM, Jul 04, 2008

I am trying to determine if a string of text contains a substring of text.

How do I do this in Liquid?

Just spent over an hour looking for the answer.

Say I have 2 variables containing text: the variables hay and needle.

For the sake of providing a working example, let’s say I assign text to these variables, like so:

{% assign hay = 'caroline' %}
{% assign needle = 'line' %}

How do I know if my needle is my hay ?

{% if hay contains needle %} 

... does not work of course.

---

Caroline from http://11heavens.com

Christocracy

Member

09:23PM, Jul 04, 2008

You could use javascript.


{% assign hay = "caroline" %}
{% assign needle = "line" %}

<script type="text/javascript">

var hay = "{{hay}}";
var needles = hay.match("{{needle}}");
alert('match: ' + needles[0]);

</script>

Caroline Schnapp

Member

09:38PM, Jul 04, 2008

The functionality I am getting at is very basic, so I’d rather use Liquid for it, if possible.

I am trying to read the URL from within collection.liquid. The URL contains a tag filter.

Let me give you an example of such URL:

http://shop.myshopify.com/collections/playback-graphics/compilation

From within collection.liquid, I must determine if the last argument in the URL is ‘compilation’ as the markup will be very different on that page in that’s the case. ‘compilation’ is a tag, it is the tag used to filter the collection.

The following will output /collections/playback-graphics:

{{ collection.url }}

So here’s what I need to set as condition:

{% capture myURL %}{{ collection.url }}/compilation{% endcapture %}
{% if myURL == 'collections/playback-graphics/compilation' %}
  <p>Hello, I am on a page where I show compilations.</p>
{% endif %}

The problem with this is that my mid argument in the URL (‘playback-graphics’) must be a wildcard. It could any number of 3 things for now. So the condition will be longer…

{% capture myURL %}{{ collection.url }}/compilation{% endcapture %}
{% if myURL == '/collections/playback-graphics/compilation' or myURL == '/collections/effects-shots/compilation' or myURL ==  '/collections/miscellaneous/compilation' %}
  <p>Hello, I am on a page where I show compilations.</p>
{% endif %}
---

Caroline from http://11heavens.com

Caroline Schnapp

Member

10:00PM, Jul 04, 2008

Christocracy, I sure will use JavaScript. (By the way I did not know we could use a method like .match on a JavaScript String…). But I would rather not use it for functionality that can break the site if JavaScript is disabled, if at all possible. Within reason. :-)

@ Anyone who stumbles over this…

The capture tag is very handy. What it does is assign to a variable whatever comes between the opening and end tag.

Examples of use:

{% capture myVariable1 %}Hello!{% endcapture %}
<p>{{ myVariable1 }}</p>
{% capture myVariable2 %}Hello, the name of my store is {{ shop.name }}.{% endcapture %}
<p>{{ myVariable2 }}</p>

Will output…

Hello!
Hello, the name of my store is myShop.
---

Caroline from http://11heavens.com

Caroline Schnapp

Member

10:45PM, Jul 04, 2008

NO! My solution does not work at all.

I still cannot read the ‘active’ tag. The filter tag.

Oh my god..

---

Caroline from http://11heavens.com

Caroline Schnapp

Member

10:47PM, Jul 04, 2008

I really don’t see a non-javascript solution for this.

---

Caroline from http://11heavens.com

Caroline Schnapp

Member

10:51PM, Jul 04, 2008

For this hay:

<a title="Show products matching tag comp-1" href="/collections/playback-graphics/comp-1"><span class="active">comp-1</span></a> <a title="Show products matching tag compilation" href="/collections/playback-graphics/compilation">compilation</a> <a title="Show products matching tag space" href="/collections/playback-graphics/space">space</a>

I may look for that needle:

<span class="active">comp-

That is SUPER ugly solution.

I just need to find the Liquid output that gives me the active tag/filter.

---

Caroline from http://11heavens.com

Caroline Schnapp

Member

11:20PM, Jul 04, 2008

Solution to the hay and needle problem: the use of includes.

Thank you, Tobi.

{% if whatever includes 'tobi' %} 
  hi tobias
{% endif %}
---

Caroline from http://11heavens.com

HunkyBill

Member

02:59AM, Jul 05, 2008

Heya,

If you compare what you can do with Liquid, and what you can do with Liquid in Shopify, you’ll notice there is a gap in implementation… odd eh… wish it was not so…

Christina

Miss Manners

07:15PM, Jul 05, 2008

Hi Caroline,

Stimulating conversation you had there with yourself!

How about this (untested, but similar things have worked for me):

{% capture standard_display %}
    [all that dull un-needlelike html]
{% endcapture %}

{% if current_tags.size > 0 %}
    {% for link in linklists.needle.links %}
        {% if current_tags.first == link.title %}
            [that sharp needle code]
        {% else %}
            {{standard_display}}
        {% endif %}
    {% endfor %}
{% else %}
    {{standard_display}}
{% endif %}

requires making a linklist called “needle”, which contains the titles of your exceptional code pages.

current_tags is handy little thing; corresponds to segment 3 of the url
---

operation absurdist feature requests: terminated

(http://en.wikipedia.org/wiki/Absurdism)

Caroline Schnapp

Member

09:08PM, Jul 05, 2008

current_tags is handy little thing; corresponds to segment 3 of the url

No way! — I thought. And then I added this simplified snippet to my collection.liquid template, and what do you know, it woooorks!

{% if current_tags.size > 0 %}
The tag slash filter here is: {{ current_tags.first }}
{% endif %}

Searching the Shopify wiki turned up no result:
http://wiki.shopify.com/Special:Search?search=current_tags

Thank you, I just had a major breaktrough thanks to you. My layout depends within collection.liquid upon the tag filter I get, a.k.a the current tag.

As I understand we can filter a collection (in the ur) with more than one tag, since current_tags is an array…? I will check.

---

Caroline from http://11heavens.com

Christina

Miss Manners

09:58PM, Jul 05, 2008

very welcome,
i’ve been around here for a while, and have done a lot of sleuthing and testing and toying. a number of things i’ve come across by pure chance/hardheadedness/playfulness are undocumented. if i had oodles of time, i’d write them up, but unfortunately, when playtime is up, i’ve got to forage, so i just scatter code here and there.

as far as i know, you’re now the second person to use current_tags

have fun! :)

and yes, you can filter with any number of tags. current_tags IS an array.

edited to add: this link might interest you.

---

operation absurdist feature requests: terminated

(http://en.wikipedia.org/wiki/Absurdism)

You must login to post a comment!

Don't have an account yet? Sign up for one.