1

This question has been addressed within the context of javascript and ruby by this community, but how would you accomplish the same thing using Jekyll / Liquid templating? Hypothetically, something along the lines of:

{% assign subarrays = array | split_items_by: 3 %}
Community
  • 1
  • 1
keruilin
  • 16,782
  • 34
  • 108
  • 175
  • AFAIK it is not possible to create multidimensional arrays in Liquid, would be the same to process an array by chunks of 3? – marcanuy Apr 21 '17 at 01:20

1 Answers1

0

Verbose code :

{% assign subArraySize = 3 %}

{% assign myArray = "one,two,three,four,five,six,seven" | split: "," %}
myArray = {{ myArray | inspect }}

{% assign multiArray = "" | split: "/" %}
multiArray = {{ multiArray | inspect }}

{% for element in myArray %}
  looping in myArray - forloop.index = {{ forloop.index }}

  {% assign reminder = forloop.index | modulo: subArraySize %}
  reminder : {{ reminder | inspect }}

  {% if reminder == 1 %}
  create a new empty sub array
  {% assign subArray = "" | split: "/" %}
  subArray = {{ subArray | inspect }}
  {% endif %}

  push current element in subArray
  {% assign subArray = subArray | push: element %}
  subArray = {{ subArray | inspect }}

  {% if reminder == 0 or forloop.last %}
  push subArray in multiArray if subarray length is
    {% assign multiArray = multiArray | push: subArray %}
  multiArray = {{ multiArray | inspect }}
  {% endif %}
{% endfor %}

{% for subArray in multiArray %}
 subArray :{{ subArray | inspect }}
{% endfor %}

If you already have ruby code available, you can write a tag plugin

David Jacquel
  • 51,670
  • 6
  • 121
  • 147