From de5be10955f088c2812e66377fb4778aacc7b08a Mon Sep 17 00:00:00 2001 From: visgoods Date: Thu, 25 Oct 2018 12:57:57 +0800 Subject: [PATCH 1/3] add part function in html.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit layout.html {{ part "css" }} home/index.html {{ define "home/index-css"}} {{ end }} in html/temple the defined block is global variable, so the way of "{{ block "css" }}" in layout and "{{ define "css"}}{{end}}" in home/index.html and home/about.html just one definition work, others will be over coved。 No use!!! This part function like extend the block in layout. Former-commit-id: c3e26a8227808cf2d1dc7121bcffe1a98ce3e678 --- view/html.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/view/html.go b/view/html.go index 24bb20ea..2d927ae1 100644 --- a/view/html.go +++ b/view/html.go @@ -44,6 +44,9 @@ var emptyFuncs = template.FuncMap{ "yield": func() (string, error) { return "", fmt.Errorf("yield was called, yet no layout defined") }, + "part": func() (string, error) { + return "", fmt.Errorf("block was called, yet no layout defined") + }, "partial": func() (string, error) { return "", fmt.Errorf("block was called, yet no layout defined") }, @@ -389,6 +392,15 @@ func (s *HTMLEngine) layoutFuncsFor(name string, binding interface{}) { // Return safe HTML here since we are rendering our own template. return template.HTML(buf.String()), err }, + "part": func(partName string) (template.HTML, error) { + nameTemp := strings.Replace(name, ".html", "", -1) + fullPartName := fmt.Sprintf("%s-%s", nameTemp, partName) + buf, err := s.executeTemplateBuf(fullPartName, binding) + if err != nil { + return "", nil + } + return template.HTML(buf.String()), err + }, "current": func() (string, error) { return name, nil }, From b5a4cf901dc185e7dc6220b1e3bcc36ffd4f148e Mon Sep 17 00:00:00 2001 From: chenPengXu Date: Fri, 26 Oct 2018 14:15:29 +0800 Subject: [PATCH 2/3] add template_html_5 for fund part in html.go Former-commit-id: f2143a7aded3c026da5eb0755630e7dd2cb0f874 --- _examples/view/template_html_5/main.go | 31 +++++++++++++++++++ .../view/template_html_5/views/about.html | 14 +++++++++ .../view/template_html_5/views/home.html | 11 +++++++ .../view/template_html_5/views/layout.html | 11 +++++++ .../template_html_5/views/user/index.html | 10 ++++++ 5 files changed, 77 insertions(+) create mode 100644 _examples/view/template_html_5/main.go create mode 100644 _examples/view/template_html_5/views/about.html create mode 100644 _examples/view/template_html_5/views/home.html create mode 100644 _examples/view/template_html_5/views/layout.html create mode 100644 _examples/view/template_html_5/views/user/index.html diff --git a/_examples/view/template_html_5/main.go b/_examples/view/template_html_5/main.go new file mode 100644 index 00000000..9fcc7fdb --- /dev/null +++ b/_examples/view/template_html_5/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "github.com/kataras/iris" +) + +func main() { + app := iris.New() + + app.RegisterView(iris.HTML("./views", ".html").Layout("layout.html")) + // TIP: append .Reload(true) to reload the templates on each request. + + app.Get("/home", func(ctx iris.Context) { + ctx.ViewData("title", "Home page"); + ctx.View("home.html") + // Note that: you can pass "layout" : "otherLayout.html" to bypass the config's Layout property + // or view.NoLayout to disable layout on this render action. + // third is an optional parameter + }) + + app.Get("/about", func(ctx iris.Context) { + ctx.View("about.html") + }) + + app.Get("/user/index", func(ctx iris.Context) { + ctx.View("user/index.html") + }) + + // http://localhost:8080 + app.Run(iris.Addr(":8080")) +} diff --git a/_examples/view/template_html_5/views/about.html b/_examples/view/template_html_5/views/about.html new file mode 100644 index 00000000..b581a12a --- /dev/null +++ b/_examples/view/template_html_5/views/about.html @@ -0,0 +1,14 @@ +{{ define "about-head"}} + +{{ end }} + +{{ define "about-body"}} + extend body content in layout. +{{ end }} +
+ Hello about page +
\ No newline at end of file diff --git a/_examples/view/template_html_5/views/home.html b/_examples/view/template_html_5/views/home.html new file mode 100644 index 00000000..36599009 --- /dev/null +++ b/_examples/view/template_html_5/views/home.html @@ -0,0 +1,11 @@ +{{ define "home-head"}} + {{.title}} + +{{ end }} +
+ Hello home page +
\ No newline at end of file diff --git a/_examples/view/template_html_5/views/layout.html b/_examples/view/template_html_5/views/layout.html new file mode 100644 index 00000000..c374205f --- /dev/null +++ b/_examples/view/template_html_5/views/layout.html @@ -0,0 +1,11 @@ + + +{{ part "head" }} + + +

[layout] Body content is below...

+ {{ part "body" }} + + {{ yield }} + + diff --git a/_examples/view/template_html_5/views/user/index.html b/_examples/view/template_html_5/views/user/index.html new file mode 100644 index 00000000..6c73fb2e --- /dev/null +++ b/_examples/view/template_html_5/views/user/index.html @@ -0,0 +1,10 @@ +{{ define "user/index-head"}} + +{{ end }} +
+ Hello user index page +
\ No newline at end of file From 20fa06b46b0ea8261f1f45fb95e544606fccb2a9 Mon Sep 17 00:00:00 2001 From: chenPengXu Date: Sat, 27 Oct 2018 20:53:24 +0800 Subject: [PATCH 3/3] modify Former-commit-id: 3cae751d13b1c97dae0db7802b42b462f7c89fdb --- _examples/view/template_html_5/views/about.html | 1 + 1 file changed, 1 insertion(+) diff --git a/_examples/view/template_html_5/views/about.html b/_examples/view/template_html_5/views/about.html index b581a12a..a2642992 100644 --- a/_examples/view/template_html_5/views/about.html +++ b/_examples/view/template_html_5/views/about.html @@ -1,4 +1,5 @@ {{ define "about-head"}} + about page