commit
854a90145b
@ -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,
|
||||
})
|
||||
}
|
||||
|
@ -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)
|
||||
|
||||
|
9
views/about.html
Normal file
9
views/about.html
Normal 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}}
|
@ -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
13
views/home.html
Normal 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}}
|
Loading…
Reference in New Issue
Block a user