# For loop in 3 columns

**URL:** <https://community.silverfin.com/t/for-loop-in-3-columns/2537>\
**Category:** Templates\
**Created:** [November 14, 2024, 6:57am UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537 "2024-11-14T06:57:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abo](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@Abo](https://community.silverfin.com/u/Abo)\
**Post date:** [November 14, 2024, 6:57am UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/1 "2024-11-14T06:57:46Z")

</div>

Hi,  
in the below ‘for loop’ the data is stored into one column, however if would like to split up the data in 3 columns, with a max of 20 items per column, could you help me further with this?

Thank you.  
See the code below:

{% ic %}{::infotext}

{% stripnewlines %}

 {% for tax\_title in tax\_titles %} {% if titles\_with\_main contains tax\_title %} {% capture main\_title %}{{ tax\_title }}\_main{% endcapture %} {% capture main\_title\_translation %}t\_{{[main\_title]}}{% endcapture %} {% if main\_titles\_shown\_info contains [main\_title] %} {% assign show\_main\_title\_info = false %} {% else %} {% assign show\_main\_title\_info = true %} {% assign main\_titles\_shown\_info = main\_titles\_shown\_info | append: [main\_title] | append: ";" %} {% endif %} {% if show\_main\_title\_info %} {% endif %} {% endif %} {% capture translation %}t\_{{ tax\_title }}{% endcapture %} {% capture sub\_categories %}{{ tax\_title }}\_list{% endcapture %} {% if titles\_with\_main contains tax\_title %} {% else %} {% endif %} {% endfor %} 

| **{::font size="xl"}{% t main\_title\_translation %}{:/font}** |
| {% input custom.tax\_memo.[tax\_title] as:boolean %} | {% linkto period.reconciliations.ey\_cit\_additional\_remarks\_eng target:tax\_title %}{% t translation %}{% endlinkto %} | {% linkto period.reconciliations.ey\_cit\_additional\_remarks\_eng target:tax\_title %}{::font size="l"}{% t translation %}{:/font}{% endlinkto %} |

{% endstripnewlines %}

{:/infotext}{% endic %}

---

<div class="post-metadata">

**Author:** ![robindeclercq](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/robindeclercq/32/472_2.png) [@robindeclercq](https://community.silverfin.com/u/robindeclercq)\
**Post date:** [November 15, 2024, 12:35pm UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/2 "2024-11-15T12:35:21Z")

</div>

Hi @Abo ,

Thank you for raising your question here!

Before we can guide you into the right direction, we do have a couple of questions.

The code example you provided is a little bit difficult to understand in the context of your question, would you be able to provide perhaps a simplified example or wireframe of what you want to accomplish? Also, when sharing code it’s best to use the `Preformatted text` as otherwise some of your styling code gets applied in your text.

In case you are looking into a way to loop through an array and present you data in a certain format, you could play around with the [limit](https://developer.silverfin.com/docs/iterations#offset) and [offset](https://developer.silverfin.com/docs/iterations#limit) tags perhaps?

Happy to further assist you!

Kind regards,  
Robin

---

<div class="post-metadata">

**Author:** ![Abo](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@Abo](https://community.silverfin.com/u/Abo)\
**Post date:** [November 15, 2024, 1:05pm UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/3 "2024-11-15T13:05:20Z")

</div>

Hi,  
See current example of the table (image included)

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/getsilverfin/original/2X/d/d81c64c381c30611784837e2d8b50b29188d1733.jpeg)

The for loop exports the data between the “td” into 1 column, however I want to split the data in 3 columns with max 20 items per column.

```auto
<table class="usr-width-100">
  <tbody>
    {% for tax_title in tax_titles %}
      {% if titles_with_main contains tax_title %}
        {% capture main_title %}{{ tax_title }}_main{% endcapture %}
        {% capture main_title_translation %}t_{{[main_title]}}{% endcapture %}
        {% if main_titles_shown_info contains [main_title] %}
          {% assign show_main_title_info = false %}
        {% else %}
          {% assign show_main_title_info = true %}
          {% assign main_titles_shown_info = main_titles_shown_info | append: [main_title] | append: ";" %}
        {% endif %}
        {% if show_main_title_info %}
            <tr>
              <td class="usr-indent-3" colspan="2"><b>{::font size="xl"}{% t main_title_translation %}{:/font}</b></td>
            </tr>
        {% endif %}
      {% endif %}
      {% capture translation %}t_{{ tax_title }}{% endcapture %}
      {% capture sub_categories %}{{ tax_title }}_list{% endcapture %}
      <tr>
        <td class="usr-width-5">{% input custom.tax_memo.[tax_title] as:boolean %}</td>
        {% if titles_with_main contains tax_title %}
        <td class="usr-width-95">{% linkto period.reconciliations.ey_cit_additional_remarks_eng target:tax_title %}{% t translation %}{% endlinkto %}</td>
        {% else %}
        <td class="usr-width-95">{% linkto period.reconciliations.ey_cit_additional_remarks_eng target:tax_title %}{::font size="l"}{% t translation %}{:/font}{% endlinkto %}</td>
        {% endif %}
      </tr>
    {% endfor %}
  </tbody>
</table>

```

---

<div class="post-metadata">

**Author:** ![robindeclercq](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/robindeclercq/32/472_2.png) [@robindeclercq](https://community.silverfin.com/u/robindeclercq)\
**Post date:** [November 15, 2024, 1:49pm UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/4 "2024-11-15T13:49:15Z")

</div>

Hi @Abo ,

Thanks for the swift and clear response!

The additional explanation and code example make your question more clear.

So I think what you need is as I mentioned in my previous reply the use of the tag `limit` and `offset`. I made a simplified example here:

```auto
{% assign tax_titles = "1|2|3|4|5|6|7|8|9" | split:"|" %}
{% assign offset_i = 0 %}

<table class="usr-width-100">
  <tbody>
    {% for i in (0..2) %}
      <tr>
        <td class=""></td>
        {% for tax_title in tax_titles limit:3 offset:INT(offset_i) %}
          <td class="usr-width-5">{% input custom.tax_memo.[tax_title] as:boolean %}</td>
          <td class="usr-width-25">{{ tax_title }}</td>
        {% endfor %} 
      </tr>
      {% assign offset_i = offset_i+3 %}
    {% endfor %}
  </tbody>
</table>

```

A little breakdown and a few notes on the example on how it can apply in your case:

- So I start with looping through the rows. In the example I only need 3 rows as the total amount of elements is 9 (so 3 rows with 3 elements). In your case if the amount is always 20 it could be `(0..19)`, or dynamic if the number of elements change

- Then for each row we loop through the `tax_titles`, here is where we start using the `limit` and `offset`.  
- I’m setting a fixed limit of 3 to indicate I only want to have 3 columns on each row;  
- Next to that, I also indicate where in the `tax_titles ` array I want to start. So for the first row, the 3 first items of the `tax_titles ` array should be displayed, for the second row the items 4,5,6 should be displayed (so `offset_i` is 4), and so on.

- I have also added an empty first column for each iteration, this is to make sure the total width amounts to 100 (as you cannot divide 100 over 3 columns). You can also add this empty column to the end or divide the `usr-width` of the other columns a bit differently.

- The most important thing to keep in mind is that each row should have the same amount of columns or the table will error (struggled a bit myself when I was prepping the example 😅). In case you end up with 2 columns of 20 items and 1 column of 19 items you will need to add an empty column in that last row.

Does this example provide enough support to apply in your use-case?

Happy to further assist if needed!

Kind regards,  
Robin

---

<div class="post-metadata">

**Author:** ![Abo](https://avatars.discourse-cdn.com/v4/letter/a/fbc32d/32.png) [@Abo](https://community.silverfin.com/u/Abo)\
**Post date:** [November 15, 2024, 2:34pm UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/5 "2024-11-15T14:34:29Z")

</div>

Hi,  
It’s already helpful!  
But I’m still struggling to get your code implemented in my snippet. Would it be possible to implement your part into it?  
Secondly, I want to have the 2nd title below the first, not in the next column.  
Meaning 1 to 3 in the first column, 4 to 6 in the second column, …  
Otherwise it’s a lot of work to maintain.

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/getsilverfin/original/2X/3/39c61ccb245738419cdf30b69a734484b8b9034a.png)

Thx a lot!

---

<div class="post-metadata">

**Author:** ![robindeclercq](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.silverfin.com/robindeclercq/32/472_2.png) [@robindeclercq](https://community.silverfin.com/u/robindeclercq)\
**Post date:** [November 15, 2024, 3:20pm UTC](https://community.silverfin.com/t/for-loop-in-3-columns/2537/6 "2024-11-15T15:20:57Z")

</div>

Hi @Abo ,

Okay, first if element 1 to 3 should be in the first column, 4 to 6 in the second column and so on, the code approach needs to change a bit. Please have a look at my updated example:

```auto
{% assign tax_titles = "1|2|3|4|5|6|7|8|9" | split:"|" %}
{% assign offset_i = 0 %}
{% assign element = 0 %}

<table class="usr-width-100">
  <tbody>
    {% for i in (0..2) %}
      {% assign element = INT(forloop.index0) %}
      <tr>
        <td class=""></td>
        {% for tax_title in tax_titles limit:3 %}
          <td class="usr-width-5">{% input custom.tax_memo.[tax_title] as:boolean %}</td>
          <td class="usr-width-25">{{ tax_titles[element] }}</td>
          {% assign element = INT(element+3) %}
        {% endfor %} 
      </tr>
    {% endfor %}
  </tbody>
</table>

```

So in this case we do not need the `offset` tag anymore and the approach is structurally a bit different. Let me elaborate:

- Still we loop through a number of rows. As with the previous example this number can be fixed but also dynamic depending on how your table will be build

- For each of the rows, we’ll need to print 3 elements of the `tax_titles`. Also that part is unchanged

- Now which items to print where, that has changed a bit. We will have to print a specific element in each column for each row. To determine the correct element I have attached this to the forloop index. So for the first row, the first column element 0 will be printed, the column next to that element 3 will be printed, and next to that element 6. When a new row starts, we start with element 1, which will be element 4 in column next to it, and so on.

Happy to try and merge this into your use-case but it’s a bit hard since I have no view on what the `tax_titles` array entails. Also not sure about what this main title is actually or what it should do 😅.

I would suggest trying to implement what I now shared in your code yourself first, but more than happy to help out if needed 👍 .

Enjoy the weekend!

Kind regards,  
Robin
