Skip to content

Instantly share code, notes, and snippets.

@ykyuen
Created November 19, 2018 06:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykyuen/b5ed8de60c2c8ff7ddd717b45e05fb90 to your computer and use it in GitHub Desktop.
Save ykyuen/b5ed8de60c2c8ff7ddd717b45e05fb90 to your computer and use it in GitHub Desktop.
setup-nested-html-template-in-go-echo-web-framework-05
package main
import (
"html/template"
"io"
"github.com/labstack/echo"
"gitlab.com/ykyuen/golang-echo-template-example/handler"
)
// Define the template registry struct
type TemplateRegistry struct {
templates *template.Template
}
// Implement e.Renderer interface
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
// Echo instance
e := echo.New()
// Instantiate a template registry and register all html files inside the view folder
e.Renderer = &TemplateRegistry{
templates: template.Must(template.ParseGlob("view/*.html")),
}
// Route => handler
e.GET("/", handler.HomeHandler)
// Start the Echo server
e.Logger.Fatal(e.Start(":1323"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment