Skip to content

Commit

Permalink
Add advanced example source code (#15)
Browse files Browse the repository at this point in the history
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
sairoutine authored and appleboy committed Jun 7, 2018
1 parent d2d1ac3 commit 41d1d62
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 9 deletions.
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import "github.com/gin-contrib/multitemplate"

### Simple example

See [example/example.go](example/example.go)
See [example/simple/example.go](example/simple/example.go)

[embedmd]:# (example/example.go go)
[embedmd]:# (example/simple/example.go go)
```go
package main

Expand Down Expand Up @@ -64,11 +64,13 @@ func main() {

[Approximating html/template Inheritance](https://elithrar.github.io/article/approximating-html-template-inheritance/)

See [example/advanced/example.go](example/advanced/example.go)

[embedmd]:# (example/advanced/example.go go)
```go
package main

import (
"html/template"
"path/filepath"

"github.com/gin-contrib/multitemplate"
Expand All @@ -79,30 +81,36 @@ func main() {
router := gin.Default()
router.HTMLRender = loadTemplates("./templates")
router.GET("/", func(c *gin.Context) {
c.HTML(200, "index.tmpl", gin.H{
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/*.tmpl")
layouts, err := filepath.Glob(templatesDir + "/layouts/*.html")
if err != nil {
panic(err.Error())
}

includes, err := filepath.Glob(templatesDir + "/includes/*.tmpl")
includes, err := filepath.Glob(templatesDir + "/includes/*.html")
if err != nil {
panic(err.Error())
}

// Generate our templates map from our layouts/ and includes/ directories
for _, layout := range layouts {
files := append([]string{layout}, includes...)
r.Add(filepath.Base(layout), template.Must(template.ParseFiles(files...)))
for _, include := range includes {
files := append(layouts, include)
r.AddFromFiles(filepath.Base(include), files...)
}
return r
}
Expand Down
46 changes: 46 additions & 0 deletions example/advanced/example.go
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
}
1 change: 1 addition & 0 deletions example/advanced/templates/includes/article.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "content"}}Hi, this is article template{{end}}
1 change: 1 addition & 0 deletions example/advanced/templates/includes/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{define "content"}}Hi, this is index.html{{end}}
3 changes: 3 additions & 0 deletions example/advanced/templates/layouts/base.html
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.

0 comments on commit 41d1d62

Please sign in to comment.