is it possible to specify an Array index range in for loops?

Highlighted
New Contributor

In Javascript for example, you can use the slice function, 

array.slice(begin [, end])

to specify a beginning and end index of an array. This can serve as basically a filter to get values from the begin value to the end value.

In Ruby, this functionality is present in the Array index range:

array[1..3]

Is there an equivalent for HUBL? I'm basically trying to create 3 for loops in a row for each +3 items in an array. so (in pseudocode)

{% for x in array[1..2] %}
  <some html here />
{% endfor %}
{% for x in array[3..5] %} <some html here /> {% endfor %} {% for x in array[6..8] %} <some html here /> {% endfor %}

Please help! Can't find anything for this. Seems like relatively common functionality that should be included, if it isn't already.

Reply
0 Upvotes
1 Reply 1
Advisor

Hey @SpectrussJacob 

 

This sounds like a problem that might possible suit the batch filter.

 

{% set ar = ["a","b","c","d","e","f","g","h"] %}

<table>
{% for row in ar|batch(3) %}
   <tr>
    {% for column in row %}
        <td>{{ column }}</td>
    {% endfor %}
    </tr> 
{% endfor %}
</table>

Will output:

<table>
   <tbody><tr>
        <td>a</td>
        <td>b</td>
        <td>c</td>
    </tr> 
   <tr>
        <td>d</td>
        <td>e</td>
        <td>f</td>
    </tr> 
   <tr>
        <td>g</td>
        <td>h</td>
        <td></td>
    </tr> 
</tbody></table>

And at a glance i'm positive there is at least one more solution that might work for you, but it would be substantially more verbose.

Reply
0 Upvotes