Last active
September 23, 2017 01:18
-
-
Save pinscript/1ee331db9f5bf0d577d6 to your computer and use it in GitHub Desktop.
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 "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}} | |
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"}} | |
<!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}} | |
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 ( | |
"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") | |
} |
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 "body"}} | |
Viewing user ID {{.UserId}}. | |
{{end}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment