Skip to content

Instantly share code, notes, and snippets.

@pinscript
Last active September 23, 2017 01:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pinscript/1ee331db9f5bf0d577d6 to your computer and use it in GitHub Desktop.
Save pinscript/1ee331db9f5bf0d577d6 to your computer and use it in GitHub Desktop.
{{define "body"}}
This is the start page.
<br><br>
Check out <a href="/user/5">user 5</a> or <a href="/user/7">user 7</a>.
{{end}}
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Layout test</title>
</head>
<body>
<div class="container">
<h1>Header</h1>
{{template "body" .}}
<h1>Footer</h1>
</div>
</body>
</html>
{{end}}
package main
import (
"html/template"
"github.com/alexandernyquist/gin"
)
type ViewData struct {
UserId string
}
func main() {
r := gin.Default()
r.GET("", func (g *gin.Context) {
r.HTMLTemplates = template.Must(template.ParseFiles("templates/layout.html", "templates/home.html"))
g.HTML(200, "base", nil)
})
r.GET("/user/:id", func (g *gin.Context) {
r.HTMLTemplates = template.Must(template.ParseFiles("templates/layout.html", "templates/view.html"))
g.HTML(200, "base", ViewData{g.Params.ByName("id")})
})
r.Run(":8082")
}
{{define "body"}}
Viewing user ID {{.UserId}}.
{{end}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment