Declaring a `list`
To declare a new list variable, you can use the following syntax:
-- <data-type> list <list-name>:
-- <value>
-- <value>
-- <value>
-- ...
-- end: <list-name>
Also make sure to use the end syntax `-- end: ` to mark
the end of the list during initialization.
For example, to create a list of strings called `weekdays`, you would use the
following syntax:
-- string list weekdays:
-- string: Sunday
-- string: Monday
-- string: Tuesday
-- string: Wednesday
-- string: Thursday
-- string: Friday
-- string: Saturday
-- end: weekdays
This creates a new variable called `weekdays`, which is a list of `string`s. The
list is initialized with seven strings representing the days of the week.
By default, lists in `fastn` are immutable, which means that their contents
cannot be changed after they are initialized. However, you can make a list
mutable by prefixing it with a `$` symbol, like this:
-- string list $weekdays:
-- end: $weekdays
Let's checkout the list declaration for the more complex type data like record.
-- record person:
caption name:
body bio:
-- person list people:
-- person: Amit Upadhyay
Amit is CEO of FifthTry.
-- person: Shobhit Sharma
Shobhit is a developer at FifthTry.
-- end: people
Here we have created a `list` of `person` objects, called it `people`, and
created two `person` objects and inserted them the `people` list.
`ftd.ui` type as a list
You can use [`ftd.ui list`](ftd/built-in-types/#ftd-ui) type or
[`children`](ftd/built-in-types/#children) type to pass a UI component in a
list.
-- ftd.ui list uis:
-- ftd.text: Hello World!
color: $inherited.colors.text-strong
-- ftd.text: I love `fastn`.
color: $inherited.colors.text-strong
-- end: uis
-- foo:
-- ftd.text: Hello World!
color: $inherited.colors.text-strong
-- ftd.text: I love `fastn`.
color: $inherited.colors.text-strong
-- end: foo
-- component foo:
children uis:
... some code here
-- end: foo
Accessing list items
Once you have created a list, you can access its elements using indexing or
looping.
Once you have created a list, you can access its items using their index. In
`fastn`, indexing starts at 0. Or you can use
Using `loop`
You can also access the elements of a list using a loop. In `fastn`, you can use
the `$loop$` keyword to iterate over a list. Here's an example:
-- ftd.text: $obj
$loop$: $weekdays as $obj
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
This code will output each element of the `weekdays` list on a separate line.
Similarly, To iterate over a `uis` list, you would use the following syntax:
-- obj:
$loop$: $uis as $obj
Hello World!
I love `fastn`.
Using index
You can access an element of a list by its index. In `fastn`, list indexing is
zero-based, which means the first element of a list has an index of 0, the
second element has an index of 1, and so on. You can use the `.` operator to
access an element of a list by its index.
For example, to access the first item in the `weekdays` list, you would use the
following syntax:
-- ftd.text: $weekdays.0
color: $inherited.colors.text-strong
Similarly, To access the first component in the `uis` list, you would use the
following syntax:
`$processor$`
A list can be created using platform provided processors:
-- string list foo:
$processor$: some-list
Here the value of the list will be provided by the `some-list` processor.
If we already have a list we can insert values to it using `$processor$` as
well:
-- string list $foo:
-- end: $foo
-- $foo:
$processor$: some-list