Skip to content

Commit

Permalink
add layout key to data
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingwei committed Dec 22, 2019
1 parent ec6cebc commit fc18841
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ func YourCode() error {
}

func YourCodeTwo() error {
data := make(map[string]interface{})
data["layout"] = "layout/default.html"
// data["site"] = "https://WhereSmile.com"
return simplate.ExecuteTemplate(os.Stdout,"home/body.html", data)
}

func YourCodeThree() error {
data := make(map[string]interface{})
// data["site"] = "https://WhereSmile.com"
return simplate.ExecuteViewPathTemplateWithLayout(os.Stdout, "layout/default.html", "home/body.html", data)
Expand Down
6 changes: 6 additions & 0 deletions gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (hr *GinRendererS) Instance(name string, data interface{}) render.Render {
sugar.Warnf("no template of name: %s", name)
}

layoutFile := defaultLayoutFile

// body
var buf bytes.Buffer
ExecuteViewPathTemplate(&buf, name, data)
Expand All @@ -36,6 +38,10 @@ func (hr *GinRendererS) Instance(name string, data interface{}) render.Render {
if ok {
dataMap["LayoutContent"] = template.HTML(buf.String())
dataT = dataMap
// custom layout
if layout, ok := dataMap["layout"]; ok {
layoutFile = layout.(string)
}
} else {
dataT["LayoutContent"] = template.HTML(buf.String())
}
Expand Down
9 changes: 6 additions & 3 deletions simplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ func InitTemplate() error {

simplateViewPathTemplates = make(map[string]*template.Template)

return BuildTemplate(viewsPath)
return BuildTemplate(defaultViewsPath)
}

// ExecuteTemplate execute template with default layout file.
func ExecuteTemplate(wr io.Writer, bodyName string, data map[string]interface{}) error {
return ExecuteViewPathTemplateWithLayout(wr, layoutFile, bodyName, data)
if layout, ok := data["layout"]; ok {
return ExecuteViewPathTemplateWithLayout(wr, layout.(string), bodyName, data)
}
return ExecuteViewPathTemplateWithLayout(wr, defaultLayoutFile, bodyName, data)
}

// ExecuteViewPathTemplateWithLayout excute template with layout
Expand Down Expand Up @@ -47,5 +50,5 @@ func ExecuteViewPathTemplate(wr io.Writer, name string, data interface{}) error
}
return err
}
panic("can't find templatefile in the path:" + viewsPath + "/" + name)
panic("can't find templatefile in the path:" + defaultViewsPath + "/" + name)
}
15 changes: 12 additions & 3 deletions simplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

func TestMain(m *testing.M) {
ViewsPath = "views"

// template function
dataFormatFunc := func(t time.Time) string {
Expand All @@ -20,8 +19,8 @@ func TestMain(m *testing.M) {

// initial template
InitTemplate()
kk := m.Run()
os.Exit(kk)
xm := m.Run()
os.Exit(xm)
}
func TestSimplateViewPathTemplates(t *testing.T) {
assert.NotEqual(t, 0, len(simplateViewPathTemplates))
Expand Down Expand Up @@ -50,3 +49,13 @@ func TestExecuteTemplate(t *testing.T) {
assert.Nil(t, err)
assert.Contains(t, buf.String(), "2018年11月03日05时28分44秒UTC")
}

func TestExecuteTemplateLayout(t *testing.T) {
data := make(map[string]interface{})
data["layout"] = "layout/home.html"

var buf bytes.Buffer
err := ExecuteTemplate(&buf, "home/head.tpl", data)
assert.Nil(t, err)
assert.Contains(t, buf.String(), "home.html")
}
18 changes: 7 additions & 11 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ import (
)

var (
// public

// ViewsPath root path of views
viewsPath = "views"
// LayoutFile layout file path
layoutFile = "layout/default.html"

//
// private variables
// defaultViewsPath default root path of views
defaultViewsPath = "views"
// defaultLayoutFile default layout file path
defaultLayoutFile = "layout/default.html"

// templates
simplateViewPathTemplates = make(map[string]*template.Template)
Expand All @@ -26,10 +22,10 @@ var (

// SetViewsPath set view's path
func SetViewsPath(path string) {
viewsPath = path
defaultViewsPath = path
}

// SetLayoutFile set layout's file
func SetLayoutFile(layoutFile string) {
layoutFile = layoutFile
func SetLayoutFile(layout string) {
defaultLayoutFile = layout
}
8 changes: 8 additions & 0 deletions views/layout/home.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
</head>
<body>
<p>home.html</p>
{{ if .LayoutContent }}{{ .LayoutContent }} {{ end }}
</body>
</html>

0 comments on commit fc18841

Please sign in to comment.