all repos — homestead @ ab82828c1e746ae1b93c2e70a24b210896047601

Code for my website

refactor: make layout easier to reuse/extend

Alan Pearce
commit

ab82828c1e746ae1b93c2e70a24b210896047601

parent

59ae179681edaabaabb8ed8d981374abd3d77cda

1 file changed, 20 insertions(+), 16 deletions(-)

jump to
M templates/layout.gotemplates/layout.go
@@ -22,7 +22,7 @@ TitleAttrs Attrs
BodyAttrs Attrs } -func extendAttrs(base Attrs, attrs Attrs) g.Node { +func ExtendAttrs(base Attrs, attrs Attrs) g.Node { m := base for key, value := range attrs { if v, found := base[key]; found {
@@ -54,25 +54,19 @@ ),
StyleEl(g.Raw(CSS)), ), Body( - extendAttrs(page.BodyAttrs, nil), + ExtendAttrs(page.BodyAttrs, nil), A(Class("skip"), Href("#main"), g.Text("Skip to main content")), Header( H2( A( - extendAttrs(Attrs{ + ExtendAttrs(Attrs{ "class": "title p-name", "href": "/", }, page.TitleAttrs), g.Text(site.Title)), ), Nav( - g.Map(site.Menu, func(item config.MenuItem) g.Node { - return A( - Href(item.URL.String()), - g.If(item.URL.IsAbs(), Target("_blank")), - g.Text(item.Name), - ) - }), + g.Map(site.Menu, MenuLink), ), ), Main(ID("main"), g.Group(children)),
@@ -90,13 +84,23 @@ g.Text(" is "),
A(Href("https://opensource.org/licenses/MIT"), g.Text("MIT")), ), g.If(site.InjectLiveReload, - Script(Defer(), g.Raw(` - new EventSource("/_/reload").onmessage = event => { - console.log("got message", event) - window.location.reload() - }; - `)), + LiveReload, ), ), )) } + +var LiveReload = Script(Defer(), g.Raw(` + new EventSource("/_/reload").onmessage = event => { + console.log("got message", event) + window.location.reload() + }; +`)) + +func MenuLink(item config.MenuItem) g.Node { + return A( + Href(item.URL.String()), + g.If(item.URL.IsAbs(), Target("_blank")), + g.Text(item.Name), + ) +}