about summary refs log tree commit diff stats
path: root/examples
diff options
context:
space:
mode:
authorMarkus Wüstenberg2020-11-02 10:03:05 +0100
committerGitHub2020-11-02 10:03:05 +0100
commit6c8f0c235287edf7252fe239d4c9beb258c6ff01 (patch)
treeae1245a8e1491f5f369e8d9ef2517148d9774130 /examples
parent92ba5904c1645e6572f5ff1b9d0e0ec629e1afb9 (diff)
downloadgomponents-6c8f0c235287edf7252fe239d4c9beb258c6ff01.tar.lz
gomponents-6c8f0c235287edf7252fe239d4c9beb258c6ff01.tar.zst
gomponents-6c8f0c235287edf7252fe239d4c9beb258c6ff01.zip
Render to Writer instead of string (#39)
The Render function has been changed to take a `Writer` instead of returning a string. This makes it possible to generate documents without having the whole content in memory.

This also removes the `gomponents.Write` function, which is now redundant.

Furthermore, the `el.Document` function has been changed to only take one child, as multiple children never make sense for it. (It's not even a child, more a sibling.)
Diffstat (limited to 'examples')
-rw-r--r--examples/simple/simple.go17
1 files changed, 9 insertions, 8 deletions
diff --git a/examples/simple/simple.go b/examples/simple/simple.go
index 6683bc4..3802f43 100644
--- a/examples/simple/simple.go
+++ b/examples/simple/simple.go
@@ -14,22 +14,23 @@ func main() {
 }
 
 func handler(w http.ResponseWriter, r *http.Request) {
-	_ = g.Write(w, page(pageProps{
+	p := page(props{
 		title: r.URL.Path,
 		path:  r.URL.Path,
-	}))
+	})
+	_ = p.Render(w)
 }
 
-type pageProps struct {
+type props struct {
 	title string
 	path  string
 }
 
-func page(props pageProps) g.Node {
+func page(p props) g.Node {
 	return el.Document(
 		el.HTML(attr.Lang("en"),
 			el.Head(
-				el.Title(props.title),
+				el.Title(p.title),
 				el.Style(attr.Type("text/css"),
 					g.Raw(".is-active{font-weight: bold}"),
 					g.Raw("ul.nav { list-style-type: none; margin: 0; padding: 0; overflow: hidden; }"),
@@ -37,10 +38,10 @@ func page(props pageProps) g.Node {
 				),
 			),
 			el.Body(
-				navbar(navbarProps{path: props.path}),
+				navbar(navbarProps{path: p.path}),
 				el.Hr(),
-				el.H1(props.title),
-				el.P(g.Textf("Welcome to the page at %v.", props.path)),
+				el.H1(p.title),
+				el.P(g.Textf("Welcome to the page at %v.", p.path)),
 				el.P(g.Textf("Rendered at %v", time.Now())),
 			),
 		),