11

I have a folder of images that I'd like to render on a page. I'd like these images to be ordered/filtered a particular way. To do that, I understand that the images need to first be together in an array.

I therefore start with an empty array:

{% assign my_array = "" %}

I then loop through the image folder, and attempt different ways of pushing each image into my_array. Example:

{% for image in site.static_files %}
  {% if image.path contains "assets/images/target-folder" %}
     <!-- Push image into array -->
     {{ my_array | push: image }}
  {% endif %}
{% endfor %}

Ideally, I can then use this array as intended:

{% for image in my_array | sort:"date" | reverse %}
  <!-- show image -->
{% endfor %}

I'm aware that I could make a data file with the images, but I'd like to avoid needing to take that extra step. Thanks for reading.

Danny
  • 475
  • 6
  • 19

3 Answers3

26

You are almost there, the way of how you are creating the array it is the only thing to fix.

This {% assign my_array = "" %} creates an empty string. One easy way to create an array in Liquid is to split the above:

{% assign my_array = "" | split: ',' %}

Now you can push items into the array inside a for loop in the following way:

{% for image in site.static_files %}
  {% if image.path contains "assets/images/target-folder" %}
     <!-- Push image into array -->
     {% assign my_array = my_array | push: image %}
  {% endif %}
{% endfor %}
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • 2
    Works great, thank you. I've used your fix to later sort this array and then loop through that. Like this: `{% assign sorted_my_array = my_array | reverse %}`. – Danny Aug 29 '17 at 13:29
  • this works perfectly, BUT jekyll nor liquid has this function listed anywhere in their docs. – Edward Dec 06 '18 at 16:48
  • @user3411192 check this: https://help.shopify.com/en/themes/liquid/filters/string-filters#split – marcanuy Dec 06 '18 at 16:55
  • 1
    let me clarify: the `push` function is nowhere to be seen in Jekyll or Shopify docs. It does what it is supposed to do, but it's a secret function. – Edward Dec 07 '18 at 14:18
  • @user3411192 Sorry I get it now, look at "Array filters" in Jekyll docs: https://jekyllrb.com/docs/liquid/filters/ you can have a look at: *push* *pop* and *shift*. – marcanuy Dec 07 '18 at 16:41
  • there it is! thanks for clarifying. explains why I don't see it in the shopify docs since it's unique to only Jekyll. – Edward Dec 12 '18 at 14:43
  • Thank you, this was a lifesaver! I finally figured out how to wrangle Jenkins+Liquid into [sorting the categories case insensitively](https://gitlab.com/victor-engmark/victor-engmark.gitlab.io/-/merge_requests/54/diffs#d119e06265596c1c6aba78a5bd1b70f1f08e3057_0_8). – l0b0 Jan 30 '22 at 08:16
4

Also note that you can do this without a loop using where/where_exp filters:

  {% assign my_array = site.static_files | 
      where_exp: "item", "item.path contains 'assets/images/target-folder'" %}

or:

  {% assign target_folder = "assets/images/target-folder" %}
  {% assign my_array = site.static_files | 
      where_exp: "item", "item.path contains target_foler" %}

(Although, unlike the accepted answer, this doesn't precisely correspond to the question's title, it's still a useful option in the described example.)

Yuri P.
  • 187
  • 5
0

This solution worked for me:

{% assign namesArr = '' %}
{% for animal in animals %}
    {% assign namesArr = namesArr | append: animal.name %}
    {% if forloop.last == false %}
       {% assign namesArr = namesArr | append: "," %}
    {% endif %}
{% endfor %}

{% assign namesArr = namesArr | split: "," %}

Now namesArr is array, we can check array contains some value: https://stackoverflow.com/a/30823063/5638975

Naskalin
  • 895
  • 9
  • 9