CASE: create result tag in each loop

Let’s say we import certain data from a client, and we want to display each value of a certain column in a report template for instance:

I’ll need to create a result-tag for each loop where data has been inputted. We can do this with the capture-method and the name of this can be used as a result-tag.

Here’s the code:

<table class="usr-width-100">
  <thead>
    <tr>
      <th class="usr-width-30 usr-line-bottom"><b>{% t "Kind" %}</b></th>
      <th class="usr-width-20 usr-line-bottom"><b>{% t "Data point" %}</b></th>
      <th class="usr-line-bottom"></th>
    </tr>
  </thead>
  <tbody>
    {% fori item in custom.details_customer %}
      <tr>
        <td>{% input item.type placeholder:"Description data-point" %}</td>
        <td>{% input item.value as:currency placeholder:"Data-point value" %}</td>
        <td></td>
        {% comment %}using persisted to see i there's actually data inputted in the loop{% endcomment %}
        {% if item.persisted %}
          {% comment %}create capture to take this as the name of a result tag{% endcomment %}
          {% capture datapoint %}datapoint_{{ forloop.index }}{% endcapture %}
          {% result datapoint item.value %}
        {% endif %}
      </tr>
    {% endfori %}
  </tbody>
</table>

Let’s examine this:

{% if item.persisted %}
    ...
{% endif %}

We use the persisted method to execute code (the capture and the result) only when data has been inputted. If we would not do this, then the capture and result tag would’ve been rendered even if there’s no data in it (because in a fori-loop, your last loop will always be rendered).

{% capture datapoint %}datapoint_{{ forloop.index }}{% endcapture %} 

In each loop where there’s data, we’ll capture the text datapoint_ and add the forloop.index to it.
So in the first loop, the capture will give datapoint_1 as output. In the second datapoint_2, and so on…

{% result datapoint item.value %} 

We’ll use the capture in each loop (where there’s data - mind the persisted-method!) and use this as the name of our result-tag, and take the item.value in the result.

This means in another template we can use the result tags this way (let’s say the handle of the reconciliation template is data_points):

{% assign data_points = period.reconciliations.data_points %}

{{ data_points.results.datapoint_1  }}

{{ data_points.results.datapoint_3  }}

{{ data_points.results.datapoint_5  }} 

To link this in a report, you’ll need to add a formula-box and use the formula:

reconciliations.data_points.datapoint_1

:bulb:

In a report you’re restricted in a way, because you have to create a formula-box for each loop.
However, in a text or reconciliation template, you could create an array with all loops in it, and loop over it.

Let’s update our code with that case as well:

{% assign datapoints_array = "" %}

<table class="usr-width-100">
  <thead>
    <tr>
      <th class="usr-width-30 usr-line-bottom"><b>{% t "Kind" %}</b></th>
      <th class="usr-width-20 usr-line-bottom"><b>{% t "Data point" %}</b></th>
      <th class="usr-line-bottom"></th>
    </tr>
  </thead>
  <tbody>
    {% fori item in custom.details_customer %}
      <tr>
        <td>{% input item.type placeholder:"Description data-point" %}</td>
        <td>{% input item.value as:currency placeholder:"Data-point value" %}</td>
        <td></td>
        {% comment %}using persisted to see i there's actually data inputted in the loop{% endcomment %}
        {% if item.persisted %}
          {% comment %}create capture to take this as the name of a result tag{% endcomment %}
          {% capture datapoint %}datapoint_{{ forloop.index }}{% endcapture %} {{ datapoint }}
          {% result datapoint item.value %}
          {% comment %}create array to later use in another template and loop over it{% endcomment %}
          {% assign datapoints_array = datapoints_array | append:datapoint | append:";" %}
        {% endif %}
      </tr>
    {% endfori %}
  </tbody>
</table>

{% result 'datapoints_array' datapoints_array %}
{% assign datapoints_array = "" %}

We’ll create a variable to sue as an array.

{% assign datapoints_array = datapoints_array | append:datapoint | append:";" %} 

In each loop, our capture (datapint_1, datapoint_2, … as long as data has been inputted) will be used to create an array.
The output of it when the first 3 loops are data inputted, is:

datapoint_1;datapoint_2;datapoint_3;

If you wouldn’t use the persisted method, that’d be

datapoint_1;datapoint_2;datapoint_3;datapoint_4;

{% result 'datapoints_array' datapoints_array %} 

We’ll take this array in a result tag, to loop over it in another template:

{% assign data_points = period.reconciliations.data_points %}
{% assign datapoints_array = data_points.results.datapoints_array | split:";" %}

{% for item in datapoints_array %}
  {{ data_points.results.[item] }}
{% endfor %}

where:

{% assign datapoints_array = data_points.results.datapoints_array | split:";" %}

is used to split on the “;”.

{% for item in datapoints_array %}
... 
{% endif %}

We’ll loop over each “datapoint”. So the output of {{ item }} is actually also the name of the result-tag we used in the beginning.

{{ data_points.results.[item] }}

this will display all the results we’ve taken into a result-tag.
In each loop, we’ll use the dynamic variables for that:

This case has been updated on 03/10/2022 to include HTML tables.

1 Like

I tried this, but it hasn’t got the desired outcome.

{% stripnewlines %}
| Fase
| Naam zaakvoerders
{% newline %}
|--------
|--------
{% newline %}
{% fori itemopvolgendezaakvoerder in custom.opvolgendezaakvoerder_maatschap %}
| {% input itemopvolgendezaakvoerder.fase as:integer placeholder:“nr” %}
| {% input itemopvolgendezaakvoerder.naam as:text placeholder:“naam”%}
{% if itemopvolgendezaakvoerder.persisted %}
{% capture itemopvolgendezaakvoerderfase %}fase_{{ forloop.index }}{% endcapture %}
{% result ‘itemopvolgendezaakvoerderfase_array’ itemopvolgendezaakvoerderfase_array %}
{% endif %}
{% newline %}
{% endfori %}
{% endstripnewlines %}

De opvolging van de zaakvoerders wordt bepaald in artikel {% input custom.maatschap.opvolgingzaakvoerders as:integer placeholder:“nr” %} van de statuten en is als volgt voorzien:
Fase 1: {% input custom.itemopvolgendezaakvoerder.fase as:text default:“de huidige zaakvoerder wordt benoemd als enige zaakvoerder van de maatschap” %}

{% assign item_opvolgendezaakvoerderfase = period.reconciliations.item_opvolgendezaakvoerderfase %}
{% assign itemopvolgendezaakvoerderfase_array = item_opvolgendezaakvoerderfase.results.itemopvolgendefase_array | split:“;” %}
{% for itemopvolgendezaakvoerder in itemopvolgendezaakvoerderfase_array %}
{{ itemopvolgendezaakvoerderfase.results.[item] }}
{% endfor %}

The result wanted:

Below ‘Fase 1’… There should be Fase 2 accourding to the number of fases in the table

Hi Els

I’m not quite sure what it is you want to achieve but it seems that there is something wrong with your way of generating results as well as loading them into another template. Maybe you could do something like this:

{% stripnewlines %}
| Fase
| Naam zaakvoerders
{% newline %}
|--------
|--------
{% newline %}
{% assign itemopvolgendezaakvoerderfase_array = "" | split:";" %}
{% fori itemopvolgendezaakvoerder in custom.opvolgendezaakvoerder_maatschap %}
| {% input itemopvolgendezaakvoerder.fase as:integer placeholder:“nr” %}
| {% input itemopvolgendezaakvoerder.naam as:text placeholder:“naam”%}
{% capture itemopvolgendezaakvoerder_naam %}itemopvolgendezaakvoerder_naam_{{ forloop.index  }}{% endcapture %}
{% result itemopvolgendezaakvoerder_naam itemopvolgendezaakvoerder.naam  %}
{% if itemopvolgendezaakvoerder.persisted %}
{% capture itemopvolgendezaakvoerderfase %}fase_{{ forloop.index }}{% endcapture %}
{% push itemopvolgendezaakvoerderfase to:itemopvolgendezaakvoerderfase_array %}
{% endif %}
{% newline %}
{% endfori %}
{% endstripnewlines %}

{% result "itemopvolgendezaakvoerderfase_array" itemopvolgendezaakvoerderfase_array %}

And then the other template like this:

De opvolging van de zaakvoerders wordt bepaald in artikel {% input custom.maatschap.opvolgingzaakvoerders as:integer placeholder:'nr' %} van de statuten en is als volgt voorzien:
Fase 1: {% input custom.itemopvolgendezaakvoerder.fase as:text default:'de huidige zaakvoerder wordt benoemd als enige zaakvoerder van de maatschap' %}

{% assign itemopvolgendezaakvoerderfase_array = period.reconciliations.item_opvolgendezaakvoerderfase .results.itemopvolgendezaakvoerderfase_array %}

{% for itemopvolgendezaakvoerder in itemopvolgendezaakvoerderfase_array %}
  {% capture itemopvolgendezaakvoerder_naam %}itemopvolgendezaakvoerder_naam_{{ forloop.index }}{% endcapture %}
  {{ period.reconciliations.item_opvolgendezaakvoerderfase.results.[itemopvolgendezaakvoerder_naam] }}
{% endfor %}

Kind regards
Thomas