Add: templates

This commit is contained in:
Óscar M. Lage 2022-03-19 00:20:31 +01:00
parent 35ca8cc5ed
commit 4e3fcb2c17
5 changed files with 83 additions and 17 deletions

View File

@ -8,22 +8,22 @@ import (
) )
func WikiHome(c echo.Context) error { 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() res, err := store.GetAllPages()
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
} }
fmt.Println(res) fmt.Printf("res: %T", res)
return c.String(http.StatusOK, "WikiListPages") 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,
})
} }

View File

@ -1,11 +1,31 @@
package server package server
import ( import (
"log"
"errors"
"html/template"
"io"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/oscarmlage/wikingo/model" "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 ( var (
store model.Store store model.Store
) )
@ -24,9 +44,17 @@ func Serve() {
// Echo instance // Echo instance
e := echo.New() 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 // Routes
e.GET("/", WikiHome) e.GET("/", WikiHome)
e.GET("/list", WikiListPages)
e.GET("/about", WikiAbout) e.GET("/about", WikiAbout)
e.GET("/about/:id", WikiAbout) e.GET("/about/:id", WikiAbout)

9
views/about.html Normal file
View File

@ -0,0 +1,9 @@
{{define "title"}}
Wikingo | {{index . "name"}}
{{end}}
{{define "body"}}
<h1>Wikingo | {{index . "name"}}</h1>
<h2>{{index . "msg"}}</h2>
<p>About Wikingo, Lorem ipsum...</p>
{{end}}

View File

@ -1 +1,17 @@
<h1>Wikingo</h1> {{define "base.html"}}
<!DOCTYPE html>
<html>
<head>
<title>{{template "title" .}}</title>
</head>
<body>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
{{template "body" .}}
</body>
</html>
{{end}}

13
views/home.html Normal file
View File

@ -0,0 +1,13 @@
{{define "title"}}
Wikingo | {{index . "name"}}
{{end}}
{{define "body"}}
<h1>Wikingo | {{index . "name"}}</h1>
<h2>{{index . "msg"}}</h2>
<ul>
{{ range .pages }}
<li>{{ .Code }}.- {{ .Price }}</li>
{{ end }}
</ul>
{{end}}