-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add advanced example source code (#15)
Hi! From #14, I think it's difficult to understand advanced example in readme. So that I wrote a working advanced example. Thanks.
- Loading branch information
1 parent
d2d1ac3
commit 41d1d62
Showing
9 changed files
with
68 additions
and
9 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"path/filepath" | ||
|
||
"github.com/gin-contrib/multitemplate" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func main() { | ||
router := gin.Default() | ||
router.HTMLRender = loadTemplates("./templates") | ||
router.GET("/", func(c *gin.Context) { | ||
c.HTML(200, "index.html", gin.H{ | ||
"title": "Welcome!", | ||
}) | ||
}) | ||
router.GET("/article", func(c *gin.Context) { | ||
c.HTML(200, "article.html", gin.H{ | ||
"title": "Html5 Article Engine", | ||
}) | ||
}) | ||
|
||
router.Run(":8080") | ||
} | ||
|
||
func loadTemplates(templatesDir string) multitemplate.Renderer { | ||
r := multitemplate.NewRenderer() | ||
|
||
layouts, err := filepath.Glob(templatesDir + "/layouts/*.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
includes, err := filepath.Glob(templatesDir + "/includes/*.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Generate our templates map from our layouts/ and includes/ directories | ||
for _, include := range includes { | ||
files := append(layouts, include) | ||
r.AddFromFiles(filepath.Base(include), files...) | ||
} | ||
return r | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{define "content"}}Hi, this is article template{{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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{define "content"}}Hi, this is index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Title: {{ .title }} | ||
|
||
index template: {{template "content" .}} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.