all repos — gomponents @ f27cb0c05f23785189979e57c831eb798e1da1f1

HTML components in pure Go

Add Textf function (#17) Like Text, but calls fmt.Sprintf before rendering.

Markus Wüstenberg
commit

f27cb0c05f23785189979e57c831eb798e1da1f1

parent

3de9270f785082b70e5d3cb7212271d7ad86a6d9

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

changed files
M README.mdREADME.md
@@ -54,7 +54,7 @@ ),
el.Body( Navbar(path), el.H1(title), - el.P(g.Text(fmt.Sprintf("Welcome to the page at %v.", path))), + el.P(g.Textf("Welcome to the page at %v.", path)), ), ), )
M gomponents.gogomponents.go
@@ -109,6 +109,13 @@ return template.HTMLEscaper(t)
} } +// Textf creates a text DOM Node that Renders the interpolated and escaped string t. +func Textf(format string, a ...interface{}) NodeFunc { + return func() string { + return template.HTMLEscaper(fmt.Sprintf(format, a...)) + } +} + // Raw creates a raw Node that just Renders the unescaped string t. func Raw(t string) NodeFunc { return func() string {
M gomponents_test.gogomponents_test.go
@@ -81,6 +81,13 @@ assert.Equal(t, "<div/>", e)
}) } +func TestTextf(t *testing.T) { + t.Run("renders interpolated and escaped text", func(t *testing.T) { + e := g.Textf("<%v/>", "div") + assert.Equal(t, "&lt;div/&gt;", e) + }) +} + func TestRaw(t *testing.T) { t.Run("renders raw text", func(t *testing.T) { e := g.Raw("<div/>")