This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"net/http" | |
"github.com/labstack/echo" | |
"gitlab.com/ykyuen/golang-echo-template-example/model" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
type ExampleResponse struct { | |
FirstName string `json:"first_name"` | |
LastName string `json:"last_name"` | |
Msg string `json:"msg"` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
func main() { | |
// 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("view/home.html", "view/base.html")) | |
templates["about.html"] = template.Must(template.ParseFiles("view/about.html", "view/base.html")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package api | |
import ( | |
"fmt" | |
"net/http" | |
"github.com/labstack/echo" | |
"gitlab.com/ykyuen/golang-echo-template-example/model" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package model | |
type ExampleRequest struct { | |
FirstName string `json:"first_name" form:"first_name" query:"first_name"` | |
LastName string `json:"last_name" form:"last_name" query:"last_name"` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package handler | |
import ( | |
"net/http" | |
"github.com/labstack/echo" | |
) | |
func AboutHandler(c echo.Context) error { | |
// Please note the the second parameter "about.html" is the template name and should |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{define "title"}} | |
Boatswain Blog | {{index . "name"}} | |
{{end}} | |
{{define "body"}} | |
<h1>{{index . "msg"}}</h1> | |
<h2>This is the about page.</h2> | |
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{define "base.html"}} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>{{template "title" .}}</title> | |
</head> | |
<body> | |
{{template "body" .}} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"html/template" | |
"io" | |
"github.com/labstack/echo" | |
"gitlab.com/ykyuen/golang-echo-template-example/handler" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
golang-echo-template-example/ | |
├── handler/ # folder of request handlers | |
│ ├── home_handler.go # handler for home page | |
│ └── about_handler.go # handler for about page | |
├── vendor/ # dependencies managed by dep | |
│ ├── github.com/* | |
│ └── golang.org/* | |
├── view/ # folder of html templates | |
│ ├── base.html # base layout template | |
│ ├── home.html # home page template |