all repos — gomponents @ c366cfc3574daf7b31e4fa68790c29fc72f7b74b

HTML components in pure Go

Add support for additional HTML attributes in HTML5 template (#238) This PR updates the HTML5 function to accept and render additional HTML attributes. An additional test case to validate this enhancement has been added as well.

Winni Neessen
commit

c366cfc3574daf7b31e4fa68790c29fc72f7b74b

parent

d4a299fe07de5c7b4e6f1657f9474043d6074d8a

2 files changed, 15 insertions(+), 1 deletion(-)

changed files
M components/components.gocomponents/components.go
@@ -18,12 +18,13 @@ Description string
Language string Head []g.Node Body []g.Node + HTMLAttrs []g.Node } // HTML5 document template. func HTML5(p HTML5Props) g.Node { return Doctype( - HTML(g.If(p.Language != "", Lang(p.Language)), + HTML(g.If(p.Language != "", Lang(p.Language)), g.Group(p.HTMLAttrs), Head( Meta(Charset("utf-8")), Meta(Name("viewport"), Content("width=device-width, initial-scale=1")),
M components/components_test.gocomponents/components_test.go
@@ -30,6 +30,19 @@ })
assert.Equal(t, `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title></head><body></body></html>`, e) }) + + t.Run("returns an html5 document template with additional HTML attributes", func(t *testing.T) { + e := HTML5(HTML5Props{ + Title: "Hat", + Description: "Love hats.", + Language: "en", + Head: []g.Node{Link(Rel("stylesheet"), Href("/hat.css"))}, + Body: []g.Node{Div()}, + HTMLAttrs: []g.Node{Class("h-full"), ID("htmlid")}, + }) + + assert.Equal(t, `<!doctype html><html lang="en" class="h-full" id="htmlid"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Hat</title><meta name="description" content="Love hats."><link rel="stylesheet" href="/hat.css"></head><body><div></div></body></html>`, e) + }) } func TestClasses(t *testing.T) {