D
D
Developers
Search…
Traverse
A traverse flow allows a block of code to be executed once for each item in an array, and then for subsequent arrays contained within a specified key, if any. The order of iteration is guaranteed because iteration happens over sourced arrays.
The traverse flow loops through nested data recursively, flattening the output. Basic usage of the traverse tag is as follows:

Syntax

Basic syntax of traverse flow is as follows:
1
{[ traverse [key] into [value] source [source] ]}
2
{{ value }}
3
{[/]}
Copied!
With the following required attributes:
  • key - key of the iterated value within the current context
  • value - the newly created property where iteration data is stored
  • source - The source array
A traverse flow can be used to flatten nested structures, such as components. Components in Supernova are represented as a tree - parent components such as views can contain many other components (their children). This can go into unlimited depth.
In order to list all components in a hierarchy, iteration needs not only to go through the source array but also dive into all its descendants (children of the root view) and do that for unlimited depth.
Let's assume we have a variable componentTree defined that contains the following data (a simplified version of Supernova component data model):
1
"componentTree": {
2
"name": "Container",
3
"frame": "100, 100, 100, 100",
4
"components": [{
5
"name": "Subview 1",
6
"frame": "0, 0, 50, 50",
7
"components": []
8
}, {
9
"name": "Subview 2",
10
"frame": "50, 50, 50, 50",
11
"components": [{
12
"name": "Sub-sub view 1",
13
"frame": "0, 0, 50, 50",
14
"components": []
15
}]
16
}]
17
}
Copied!
The componentTree contains 4 views spread over 3 levels of depth. We can use traverse flow to allow for this nested iteration:
1
{[ traverse components into component source componentTree ]}
2
{{ shape.name }}
3
{[/]}
Copied!
The blueprint takes componentTree - the source dictionary as the first item of iteration and then checks for components key, expecting an array. If an array is encountered, the iteration continues within that leaf of the tree. The generated result will then look like this:
1
Container
2
Subview 1
3
Subview 2
4
Sub-sub view 1
Copied!
Last modified 7mo ago
Copy link
Contents
Syntax