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

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

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})