Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from caitinggui/master
Add multiple layouts templates example source code
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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("/admin", func(c *gin.Context) { | ||
c.HTML(200, "admin.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() | ||
|
||
articleLayouts, err := filepath.Glob(templatesDir + "/layouts/article-base.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
articles, err := filepath.Glob(templatesDir + "/articles/*.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Generate our templates map from our articleLayouts/ and articles/ directories | ||
for _, article := range articles { | ||
layoutCopy := make([]string, len(articleLayouts)) | ||
copy(layoutCopy, articleLayouts) | ||
files := append(layoutCopy, article) | ||
r.AddFromFiles(filepath.Base(article), files...) | ||
} | ||
|
||
adminLayouts, err := filepath.Glob(templatesDir + "/layouts/admin-base.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
admins, err := filepath.Glob(templatesDir + "/admins/*.html") | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Generate our templates map from our adminLayouts/ and admins/ directories | ||
for _, admin := range admins { | ||
layoutCopy := make([]string, len(adminLayouts)) | ||
copy(layoutCopy, adminLayouts) | ||
files := append(layoutCopy, admin) | ||
r.AddFromFiles(filepath.Base(admin), 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 admin.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 @@ | ||
{{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,3 @@ | ||
Admin Title: {{ .title }} | ||
|
||
index template: {{template "content" .}} |
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 @@ | ||
Article Title: {{ .title }} | ||
|
||
index template: {{template "content" .}} |