Getting Started
Core Concepts
Control
Interactivity
Performance
Pushing Data
HTMX Extensions
Miscellaneous
Configuration
Loops / Dealing With Lists
Very commonly you will need to render a list or slice of items onto the page.
Frameworks generally solve this in different ways, such as React uses regular JS .map function to solve it.
htmgo provides a couple of utilities to do so:
Example: Rendering a list of tasks
var items = []string{"item1", "item2", "item3"}
h.List(items, func(item string, index int) *h.Element {
if tab == TabComplete && item.CompletedAt == nil {
return h.Empty()
}
return h.Div(
h.Text(item),
)
})
Copy
1var items = []string{"item1", "item2", "item3"}
2h.List(items, func(item string, index int) *h.Element {
3 if tab == TabComplete && item.CompletedAt == nil {
4 return h.Empty()
5 }
6 return h.Div(
7 h.Text(item),
8 )
9})
Example: Rendering a map
var values = map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
h.IterMap(values, func(key string, value string) *h.Element {
return h.Div(
h.Text(key),
h.Text(value),
)
})
Copy
1var values = map[string]string{
2 "key1": "value1",
3 "key2": "value2",
4 "key3": "value3",
5}
6h.IterMap(values, func(key string, value string) *h.Element {
7 return h.Div(
8 h.Text(key),
9 h.Text(value),
10 )
11})