diff --git a/server/app.go b/server/app.go index 4fd02ad..2d3f561 100644 --- a/server/app.go +++ b/server/app.go @@ -8,22 +8,22 @@ import ( ) func WikiHome(c echo.Context) error { - res := store.GetPage() - fmt.Println(res) - return c.String(http.StatusOK, "WikiHome") -} - -func WikiAbout(c echo.Context) error { - id := c.Param("id") - return c.String(http.StatusOK, "About the wiki. id:"+id) -} - -func WikiListPages(c echo.Context) error { - fmt.Println("WikiListPages") res, err := store.GetAllPages() if err != nil { log.Panicln(err) } - fmt.Println(res) - return c.String(http.StatusOK, "WikiListPages") + fmt.Printf("res: %T", res) + return c.Render(http.StatusOK, "home.html", map[string]interface{}{ + "name": "HOME", + "msg": "This is the home page.", + "pages": res, + }) +} + +func WikiAbout(c echo.Context) error { + id := c.Param("id") + return c.Render(http.StatusOK, "about.html", map[string]interface{}{ + "name": "ABOUT", + "msg": "About Wikingo (id:)" + id, + }) } diff --git a/server/server.go b/server/server.go index 6b72265..ef8e2ba 100644 --- a/server/server.go +++ b/server/server.go @@ -1,11 +1,31 @@ package server import ( + "log" + + "errors" + "html/template" + "io" + "github.com/labstack/echo/v4" "github.com/oscarmlage/wikingo/model" - "log" ) +// Define the template registry struct +type TemplateRegistry struct { + templates map[string]*template.Template +} + +// Implement e.Renderer interface +func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + tmpl, ok := t.templates[name] + if !ok { + err := errors.New("Template not found -> " + name) + return err + } + return tmpl.ExecuteTemplate(w, "base.html", data) +} + var ( store model.Store ) @@ -24,9 +44,17 @@ func Serve() { // Echo instance e := echo.New() + // Instantiate a template registry with an array of template set + // Ref: https://gist.github.com/rand99/808e6e9702c00ce64803d94abff65678 + templates := make(map[string]*template.Template) + templates["home.html"] = template.Must(template.ParseFiles("views/home.html", "views/base.html")) + templates["about.html"] = template.Must(template.ParseFiles("views/about.html", "views/base.html")) + e.Renderer = &TemplateRegistry{ + templates: templates, + } + // Routes e.GET("/", WikiHome) - e.GET("/list", WikiListPages) e.GET("/about", WikiAbout) e.GET("/about/:id", WikiAbout) diff --git a/views/about.html b/views/about.html new file mode 100644 index 0000000..238c552 --- /dev/null +++ b/views/about.html @@ -0,0 +1,9 @@ +{{define "title"}} + Wikingo | {{index . "name"}} +{{end}} + +{{define "body"}} +

Wikingo | {{index . "name"}}

+

{{index . "msg"}}

+

About Wikingo, Lorem ipsum...

+{{end}} diff --git a/views/base.html b/views/base.html index da62155..95f378f 100644 --- a/views/base.html +++ b/views/base.html @@ -1 +1,17 @@ -

Wikingo

+{{define "base.html"}} + + + + {{template "title" .}} + + + + {{template "body" .}} + + +{{end}} diff --git a/views/home.html b/views/home.html new file mode 100644 index 0000000..8a6db1f --- /dev/null +++ b/views/home.html @@ -0,0 +1,13 @@ +{{define "title"}} + Wikingo | {{index . "name"}} +{{end}} + +{{define "body"}} +

Wikingo | {{index . "name"}}

+

{{index . "msg"}}

+ +{{end}}