Support us by giving us a
star on Github 🚀

`list`

In `fastn`, the `list` keyword can be used to create an array or list of values. The `list` keyword is followed by the data type of the values that the list will contain.

Declaring a `list`

To declare a new list variable, you can use the following syntax:
Declaring a `list`
-- <data-type> list <list-name>:

-- <value>
-- <value>
-- <value>
-- ...

-- end: <list-name>
Lang:
ftd
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:
a list of string
-- string list weekdays:

-- string: Sunday
-- string: Monday
-- string: Tuesday
-- string: Wednesday
-- string: Thursday
-- string: Friday
-- string: Saturday

-- end: weekdays
Lang:
ftd
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:
Mutable Variable
-- string list $weekdays:
-- end: $weekdays
Lang:
ftd
Let's checkout the list declaration for the more complex type data like record.
Record list
-- 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
Lang:
ftd
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.
Using `ftd.ui list` type
-- 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
Lang:
ftd
Using `children` type
-- 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
Lang:
ftd

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
Lang:
ftd
Output
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
Lang:
ftd
Output
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
Lang:
ftd
Output
Sunday
Similarly, To access the first component in the `uis` list, you would use the following syntax:
-- uis.0:
Lang:
ftd
Output
Hello World!

`$processor$`

A list can be created using platform provided processors:
Using `$processor$`
-- string list foo:
$processor$: some-list
Lang:
ftd
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
Lang:
ftd

Support `fastn`!

Enjoying `fastn`? Please consider giving us a star ⭐️ on [GitHub](https://github.com/fastn-stack/fastn) to show your support!
[⭐️](https://github.com/fastn-stack/fastn)

Getting Help

Have a question or need help? Visit our [GitHub Q&A discussion](https://github.com/fastn-stack/fastn/discussions/categories/q-a) to get answers and subscribe to it to stay tuned. Join our [Discord](https://discord.gg/a7eBUeutWD) channel and share your thoughts, suggestion, question etc. Connect with our [community](/community/)!
[💻️](/community/)

Found an issue?

If you find some issue, please visit our [GitHub issues](https://github.com/fastn-stack/fastn/issues) to tell us about it.

Quick links:

- [Install `fastn`](install/) - [Create `fastn` package](create-fastn-package/) - [Expander Crash Course](expander/) - [Syntax Highlighting in Sublime Text](/sublime/)

Join us

We welcome you to join our [Discord](https://discord.gg/a7eBUeutWD) community today. We are trying to create the language for human beings and we do not believe it would be possible without your support. We would love to hear from you.
Copyright © 2023 - fastn.com