Design
Loop over array
Repeat any part of you document
COMMUNITY FEATURE
Available for:
Carbone Cloud
Carbone On-premise
Embedded Carbone JS
v2.0+
Overview
Carbone can repeat any part of a document (rows, titles, pages...).
Just write where the i-th
and i-th+1
rows are and Carbone will automatically find the pattern to repeat, using the first row i-th
as an example.
The second line (i+1
) will be removed before rendering the result.
Only one [i+1]
tag is enough to find the repetition pattern. It is not necessary to repeat the whole [i+1]
part.
Simple array
In this example, we want to travel an array of cars.
Nested arrays
Carbone manages nested arrays (unlimited depth). Here is an example where a whole portion of a document is repeated.
As you can see, it is useless to repeat the first paragraph twice in the template, only the title of the second paragraph is necessary to help Carbone recognize where the repetition pattern of the main array {d.cars[i+1].brand}
ends.
Bi-directional loop
v4.8.0+
The bidirectional loop performs iterations in 2 directions, creating additional columns and rows.
There are a few restrictions:
- The JSON parent array must be used for rows, and the nested array must be used for columns. This limitation will be removed in Carbone v5.
- Officialy supported in DOCX, HTML, MD templates. In addition, Word can automatically resize the table to fit the page width. Bidirectional loops with other template formats are still experimental. Please get in touch with us in the chat if necessary.
Here is a real life example of a bidirectional loop.
Access the loop iterator value
v4.0.0+
Access the iterator value when a list is printed in a document.
For example: {d[i].cars[i].other.wheels[i].tire.subObject:add(.i):add(..i):add(...i)}
The number of dots corresponds to the position of the i
in the hierarchy:
...i
refers to the index value ofwheels[i]
..i
refers to the index value ofcars[i]
.i
refers to the index value ofd[i]
![WARN] The number of dots is currently inverted. It should be
...i
ford[i]
and.i
forwheels[i]
. This is a known bug. However, many users rely on the current behavior, and fixing it would break some reports. We plan to address this issue in the future while ensuring backward compatibility.