Skip to content

Instantly share code, notes, and snippets.

@oblank
Forked from pinscript/home.html
Last active August 29, 2015 14:23
Show Gist options
  • Save oblank/43bfaf68b27ce41c4c01 to your computer and use it in GitHub Desktop.
Save oblank/43bfaf68b27ce41c4c01 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