all repos — gomponents @ 267d40bbea6036f913c4047a6a2055b3e5d7bb96

HTML components in pure Go

Add Map function (#40) `Map` makes it easier to build lists of elements without having to iterate.

Markus Wüstenberg
commit

267d40bbea6036f913c4047a6a2055b3e5d7bb96

parent

6c8f0c235287edf7252fe239d4c9beb258c6ff01

1 file changed, 6 insertions(+), 6 deletions(-)

changed files
M examples/simple/simple.goexamples/simple/simple.go
@@ -61,11 +61,11 @@ {"/", "Home"},
{"/foo", "Foo"}, {"/bar", "Bar"}, } - var lis []g.Node - for _, item := range items { - lis = append(lis, el.Li(el.A(item.path, - attr.Classes(map[string]bool{"is-active": props.path == item.path}), - g.Text(item.text)))) - } + lis := g.Map(len(items), func(i int) g.Node { + item := items[i] + return el.Li( + el.A(item.path, attr.Classes(map[string]bool{"is-active": props.path == item.path}), g.Text(item.text)), + ) + }) return el.Ul(attr.Class("nav"), g.Group(lis)) }