0

I am very new to Jinja2 and I want to show 3 different things in line, repeating it 2 times so I get 3x3 block. Here is my code now:

{% for item in recommendations('someID', 3) %}

This gives me only 3 things which are displayed vertically. How can I make it to display 3 things horizontally and then repeat it 2 more times? Thank you in advance.

1 Answers1

0

If we take a function from this question's answer, and prepare our array by splitting it into a desired length chunks, so that we have array of a format [['a'. 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']], we will be able to use a simple nested for loop in Jinja to accomplish this task.

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

{% for subArray in array %}
       {% for element in subArray %}
           {{ element }}, 
       {% endfor %}
       <br />
{% endfor %}

This should result in:

a,b,c,
d,e,f,
g,h,i,
Evgenii Klepilin
  • 695
  • 1
  • 8
  • 21