diff --git a/README.md b/README.md index bc0cb4a..e3bee4b 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/gin.go b/gin.go index 46b4030..f71c910 100644 --- a/gin.go +++ b/gin.go @@ -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) @@ -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()) } diff --git a/simplate.go b/simplate.go index 6eb72a9..6804ae5 100644 --- a/simplate.go +++ b/simplate.go @@ -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 @@ -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) } diff --git a/simplate_test.go b/simplate_test.go index cfdf537..e9f6a82 100644 --- a/simplate_test.go +++ b/simplate_test.go @@ -10,7 +10,6 @@ import ( ) func TestMain(m *testing.M) { - ViewsPath = "views" // template function dataFormatFunc := func(t time.Time) string { @@ -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)) @@ -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") +} diff --git a/variables.go b/variables.go index da1a06a..eb4a814 100644 --- a/variables.go +++ b/variables.go @@ -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) @@ -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 } diff --git a/views/layout/home.html b/views/layout/home.html new file mode 100644 index 0000000..ae5db7f --- /dev/null +++ b/views/layout/home.html @@ -0,0 +1,8 @@ + + + + +

home.html

+ {{ if .LayoutContent }}{{ .LayoutContent }} {{ end }} + + \ No newline at end of file