about summary refs log tree commit diff stats
path: root/gomponents_test.go
diff options
context:
space:
mode:
authorMarkus Wüstenberg2024-09-26 11:13:11 +0200
committerMarkus Wüstenberg2024-09-26 11:13:11 +0200
commit1b2211f8727b1e50f542afffb1f89736f7c0b747 (patch)
tree4a8c9af2fa99f3afb297d6ca96f3ea7c7ecebcde /gomponents_test.go
parent3fa5e676316d3dd68fec2e99da53ca2fe0eec9c4 (diff)
downloadgomponents-1b2211f8727b1e50f542afffb1f89736f7c0b747.tar.lz
gomponents-1b2211f8727b1e50f542afffb1f89736f7c0b747.tar.zst
gomponents-1b2211f8727b1e50f542afffb1f89736f7c0b747.zip
Add ExampleMap_index
For getting the slice index using a counter outside the closure.
Diffstat (limited to 'gomponents_test.go')
-rw-r--r--gomponents_test.go12
1 files changed, 12 insertions, 0 deletions
diff --git a/gomponents_test.go b/gomponents_test.go
index c8dab1b..026cde4 100644
--- a/gomponents_test.go
+++ b/gomponents_test.go
@@ -255,6 +255,18 @@ func ExampleMap() {
 	// Output: <ul><li>party hat</li><li>super hat</li></ul>
 }
 
+func ExampleMap_index() {
+	items := []string{"party hat", "super hat"}
+	var index int
+	e := g.El("ul", g.Map(items, func(i string) g.Node {
+		e := g.El("li", g.Textf("%v: %v", index, i))
+		index++
+		return e
+	}))
+	_ = e.Render(os.Stdout)
+	// Output: <ul><li>0: party hat</li><li>1: super hat</li></ul>
+}
+
 func TestGroup(t *testing.T) {
 	t.Run("groups multiple nodes into one", func(t *testing.T) {
 		children := []g.Node{g.El("br", g.Attr("id", "hat")), g.El("hr")}