mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
Merge pull request #1117 from chenPengXu/master
add part function in html.go and examples Former-commit-id: bf22aac2abd6cf605abb1673ec346301c6c630c2
This commit is contained in:
commit
d6964acfcd
31
_examples/view/template_html_5/main.go
Normal file
31
_examples/view/template_html_5/main.go
Normal file
|
@ -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"))
|
||||||
|
}
|
15
_examples/view/template_html_5/views/about.html
Normal file
15
_examples/view/template_html_5/views/about.html
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
{{ define "about-head"}}
|
||||||
|
<title>about page</title>
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
background: #666;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
{{ define "about-body"}}
|
||||||
|
extend body content in layout.
|
||||||
|
{{ end }}
|
||||||
|
<div>
|
||||||
|
Hello about page
|
||||||
|
</div>
|
11
_examples/view/template_html_5/views/home.html
Normal file
11
_examples/view/template_html_5/views/home.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
{{ define "home-head"}}
|
||||||
|
<title>{{.title}}</title>
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
background: #999;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{{ end }}
|
||||||
|
<div>
|
||||||
|
Hello home page
|
||||||
|
</div>
|
11
_examples/view/template_html_5/views/layout.html
Normal file
11
_examples/view/template_html_5/views/layout.html
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
{{ part "head" }}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>[layout] Body content is below...</h1>
|
||||||
|
{{ part "body" }}
|
||||||
|
<!-- Render the current template here -->
|
||||||
|
{{ yield }}
|
||||||
|
</body>
|
||||||
|
</html>
|
10
_examples/view/template_html_5/views/user/index.html
Normal file
10
_examples/view/template_html_5/views/user/index.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{{ define "user/index-head"}}
|
||||||
|
<style type="text/css">
|
||||||
|
body {
|
||||||
|
background: red;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
{{ end }}
|
||||||
|
<div>
|
||||||
|
Hello user index page
|
||||||
|
</div>
|
12
view/html.go
12
view/html.go
|
@ -44,6 +44,9 @@ var emptyFuncs = template.FuncMap{
|
||||||
"yield": func() (string, error) {
|
"yield": func() (string, error) {
|
||||||
return "", fmt.Errorf("yield was called, yet no layout defined")
|
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) {
|
"partial": func() (string, error) {
|
||||||
return "", fmt.Errorf("block was called, yet no layout defined")
|
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 safe HTML here since we are rendering our own template.
|
||||||
return template.HTML(buf.String()), err
|
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) {
|
"current": func() (string, error) {
|
||||||
return name, nil
|
return name, nil
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user