Skip to content

Commit 1ea5cd5

Browse files
committed
builtin html template functions changes
1 parent abeae40 commit 1ea5cd5

File tree

115 files changed

+472
-230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+472
-230
lines changed

HISTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and
2828

2929
## Fixes and Improvements
3030

31+
- **Breaking-change**: HTML template functions `yield`, `part`, `partial`, `partial_r` and `render` now accept (and require for some cases) a second argument of the binding data context too. Convert: `{{ yield }}` to `{{ yield . }}`, `{{ render "templates/mytemplate.html" }}` to `{{ render "templates/mytemplate.html" . }}`, `{{ partial "partials/mypartial.html" }}` to `{{ partial "partials/mypartial.html" . }}` and so on.
32+
3133
- Add new `URLParamSeparator` to the configuration. Defaults to "," but can be set to an empty string to disable splitting query values on `Context.URLParamSlice` method.
3234

3335
- [PR #1992](https://github.com/kataras/iris/pull/1992): Added support for third party packages on [httptest](https://github.com/kataras/iris/tree/master/httptest). An example using 3rd-party module named [Ginkgo](github.com/onsi/ginkgo) can be found [here](https://github.com/kataras/iris/blob/master/_examples/testing/ginkgotest).

_benchmarks/view/ace/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ func index(ctx iris.Context) {
2020
}
2121

2222
ctx.ViewLayout("layouts/main")
23-
ctx.View("index", data)
23+
if err := ctx.View("index", data); err != nil {
24+
ctx.HTML("<h3>%s</h3>", err.Error())
25+
return
26+
}
2427
}

_benchmarks/view/ace/views/layouts/main.ace

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ html
33
head
44
title {{.Title}}
55
body
6-
{{ yield }}
6+
{{ yield . . }}
77
footer
88
= include partials/footer.ace .

_benchmarks/view/amber/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
2222
// On Amber this is ignored: ctx.ViewLayout("layouts/main")
2323
// Layouts are only rendered from inside the index page itself
2424
// using the "extends" keyword.
25-
ctx.View("index", data)
25+
if err := ctx.View("index", data); err != nil {
26+
ctx.HTML("<h3>%s</h3>", err.Error())
27+
return
28+
}
2629
}

_benchmarks/view/blocks/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
2222
}
2323

2424
ctx.ViewLayout("main")
25-
ctx.View("index", data)
25+
if err := ctx.View("index", data); err != nil {
26+
ctx.HTML("<h3>%s</h3>", err.Error())
27+
return
28+
}
2629
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ template "content" . }}<footer>{{ partial "partials/footer" .}}</footer></body></html>
1+
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ template "content" . }}<footer>{{ partial "partials/footer" . }}</footer></body></html>

_benchmarks/view/django/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ func index(ctx iris.Context) {
2121
// On Django this is ignored: ctx.ViewLayout("layouts/main")
2222
// Layouts are only rendered from inside the index page itself
2323
// using the "extends" keyword.
24-
ctx.View("index", data)
24+
if err := ctx.View("index", data); err != nil {
25+
ctx.HTML("<h3>%s</h3>", err.Error())
26+
return
27+
}
2528
}

_benchmarks/view/handlebars/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ func index(ctx iris.Context) {
1919
}
2020

2121
ctx.ViewLayout("layouts/main")
22-
ctx.View("index", data)
22+
if err := ctx.View("index", data); err != nil {
23+
ctx.HTML("<h3>%s</h3>", err.Error())
24+
return
25+
}
2326
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title>{{Title}}</title></head><body>{{ yield }}<footer>{{ render "partials/footer.html" .}}</footer></body></html>
1+
<!DOCTYPE html><html><head><title>{{Title}}</title></head><body>{{ yield . }}<footer>{{ render "partials/footer.html" .}}</footer></body></html>

_benchmarks/view/html/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ func index(ctx iris.Context) {
2020
}
2121

2222
ctx.ViewLayout("layouts/main")
23-
ctx.View("index", data)
23+
if err := ctx.View("index", data); err != nil {
24+
ctx.HTML("<h3>%s</h3>", err.Error())
25+
return
26+
}
2427
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ yield }}<footer>{{ render "partials/footer.html" }}</footer></body></html>
1+
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ yield . }}<footer>{{ render "partials/footer.html" . }}</footer></body></html>

_benchmarks/view/jet/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@ func index(ctx iris.Context) {
2121
// On Jet this is ignored: ctx.ViewLayout("layouts/main")
2222
// Layouts are only rendered from inside the index page itself
2323
// using the "extends" keyword.
24-
ctx.View("index", data)
24+
if err := ctx.View("index", data); err != nil {
25+
ctx.HTML("<h3>%s</h3>", err.Error())
26+
return
27+
}
2528
}

_benchmarks/view/pug/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@ func index(ctx iris.Context) {
2222
// On Pug this is ignored: ctx.ViewLayout("layouts/main")
2323
// Layouts are only rendered from inside the index page itself
2424
// using the "extends" keyword.
25-
ctx.View("index", data)
25+
if err := ctx.View("index", data); err != nil {
26+
ctx.HTML("<h3>%s</h3>", err.Error())
27+
return
28+
}
2629
}

_examples/auth/auth/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ func main() {
118118
}
119119

120120
func renderSigninForm(ctx iris.Context) {
121-
ctx.View("signin", iris.Map{"Title": "Signin Page"})
121+
if err := ctx.View("signin", iris.Map{"Title": "Signin Page"}); err != nil {
122+
ctx.HTML("<h3>%s</h3>", err.Error())
123+
return
124+
}
122125
}
123126

124127
func renderMemberPage(s *auth.Auth[User]) iris.Handler {

_examples/auth/auth/views/layouts/main.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<body>
2525
<div class="container">
2626
<main>{{ template "content" . }}</main>
27-
<footer style="position: fixed; bottom: 0; width: 100%;">{{ partial "partials/footer" .}}</footer>
27+
<footer style="position: fixed; bottom: 0; width: 100%;">{{ partial "partials/footer" . }}</footer>
2828
</div>
2929
</body>
3030
</html>

_examples/auth/goth/main.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -377,9 +377,12 @@ func main() {
377377
// ctx.View("user.html", user)
378378
//
379379
// Directly (user as .user variable):
380-
ctx.View("user.html", iris.Map{
380+
if err := ctx.View("user.html", iris.Map{
381381
"user": user,
382-
})
382+
}); err != nil {
383+
ctx.HTML("<h3>%s</h3>", err.Error())
384+
return
385+
}
383386
})
384387

385388
app.Get("/logout/{provider}", func(ctx iris.Context) {
@@ -395,11 +398,17 @@ func main() {
395398
return
396399
}
397400

398-
ctx.View("user.html", gothUser)
401+
if err := ctx.View("user.html", gothUser); err != nil {
402+
ctx.HTML("<h3>%s</h3>", err.Error())
403+
return
404+
}
399405
})
400406

401407
app.Get("/", func(ctx iris.Context) {
402-
ctx.View("index.html", providerIndex)
408+
if err := ctx.View("index.html", providerIndex); err != nil {
409+
ctx.HTML("<h3>%s</h3>", err.Error())
410+
return
411+
}
403412
})
404413

405414
// http://localhost:3000

_examples/auth/hcaptcha/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@ func register(ctx iris.Context) {
4242

4343
func registerForm(ctx iris.Context) {
4444
ctx.ViewData("SiteKey", siteKey)
45-
ctx.View("register_form.html")
45+
if err := ctx.View("register_form.html"); err != nil {
46+
ctx.HTML("<h3>%s</h3>", err.Error())
47+
return
48+
}
4649
}

_examples/bootstrapper/bootstrap/bootstrapper.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ func (b *Bootstrapper) SetupErrorHandlers() {
7777

7878
ctx.ViewData("Err", err)
7979
ctx.ViewData("Title", "Error")
80-
ctx.View("shared/error.html")
80+
if err := ctx.View("shared/error.html"); err != nil {
81+
ctx.HTML("<h3>%s</h3>", err.Error())
82+
return
83+
}
8184
})
8285
}
8386

_examples/bootstrapper/routes/index.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,8 @@ import (
77
// GetIndexHandler handles the GET: /
88
func GetIndexHandler(ctx iris.Context) {
99
ctx.ViewData("Title", "Index Page")
10-
ctx.View("index.html")
10+
if err := ctx.View("index.html"); err != nil {
11+
ctx.HTML("<h3>%s</h3>", err.Error())
12+
return
13+
}
1114
}

_examples/bootstrapper/views/shared/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<body>
1313
<div>
1414
<!-- Render the current template here -->
15-
{{ yield }}
15+
{{ yield . }}
1616
<hr />
1717
<footer>
1818
<p>&copy; 2017 - {{.AppOwner}}</p>

_examples/caddy/server1/views/shared/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</head>
66

77
<body>
8-
{{ yield }}
8+
{{ yield . }}
99
</body>
1010

1111
</html>

_examples/dropzonejs/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ func main() {
106106
// Render the actual form
107107
// GET: http://localhost:8080
108108
app.Get("/", func(ctx iris.Context) {
109-
ctx.View("upload.html")
109+
if err := ctx.View("upload.html"); err != nil {
110+
ctx.HTML("<h3>%s</h3>", err.Error())
111+
return
112+
}
110113
})
111114

112115
// Upload the file to the server

_examples/dropzonejs/README_PART2.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ func main() {
171171
app.HandleDir("/public", iris.Dir("./public"))
172172

173173
app.Get("/", func(ctx iris.Context) {
174-
ctx.View("upload.html")
174+
if err := ctx.View("upload.html"); err != nil {
175+
ctx.HTML("<h3>%s</h3>", err.Error())
176+
return
177+
}
175178
})
176179

177180
files := scanUploads(uploadsDir)

_examples/dropzonejs/src/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ func main() {
124124
app.HandleDir("/public", iris.Dir("./public"))
125125

126126
app.Get("/", func(ctx iris.Context) {
127-
ctx.View("upload.html")
127+
if err := ctx.View("upload.html"); err != nil {
128+
ctx.HTML("<h3>%s</h3>", err.Error())
129+
return
130+
}
128131
})
129132

130133
files := scanUploads(uploadsDir)

_examples/file-server/file-server/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ func uploadView(ctx iris.Context) {
9494
io.WriteString(h, strconv.FormatInt(now, 10))
9595
token := fmt.Sprintf("%x", h.Sum(nil))
9696

97-
ctx.View("upload.html", token)
97+
if err := ctx.View("upload.html", token); err != nil {
98+
ctx.HTML("<h3>%s</h3>", err.Error())
99+
return
100+
}
98101
}
99102

100103
func upload(ctx iris.Context) {

_examples/file-server/single-page-application/embedded-single-page-application/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"github.com/kataras/iris/v12"
5-
"github.com/kataras/iris/v12/x/errors"
65
)
76

87
// $ go install github.com/go-bindata/go-bindata/v3/go-bindata@latest
@@ -28,7 +27,7 @@ func newApp() *iris.Application {
2827
app.Get("/", func(ctx iris.Context) {
2928
ctx.ViewData("Page", page)
3029
if err := ctx.View("index.html"); err != nil {
31-
errors.InvalidArgument.Err(ctx, err)
30+
ctx.HTML("<h3>%s</h3>", err.Error())
3231
return
3332
}
3433
})

_examples/file-server/spa-vue-router/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ func fullVueRouter() {
2727
}
2828
2929
func index(ctx iris.Context) {
30-
ctx.View("index.html")
30+
if err := ctx.View("index.html"); err != nil {
31+
ctx.HTML("<h3>%s</h3>", err.Error())
32+
return
33+
}
3134
}
3235
*/

_examples/file-server/upload-file/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ func main() {
3131
// ctx.ViewData("", token)
3232
// or add second argument to the `View` method.
3333
// Token will be passed as {{.}} in the template.
34-
ctx.View("upload_form.html", token)
34+
if err := ctx.View("upload_form.html", token); err != nil {
35+
ctx.HTML("<h3>%s</h3>", err.Error())
36+
return
37+
}
3538
})
3639

3740
/* Read before continue.

_examples/file-server/upload-files/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ func newApp() *iris.Application {
3232
token := fmt.Sprintf("%x", h.Sum(nil))
3333

3434
// render the form with the token for any use you'd like.
35-
ctx.View("upload_form.html", token)
35+
if err := ctx.View("upload_form.html", token); err != nil {
36+
ctx.HTML("<h3>%s</h3>", err.Error())
37+
return
38+
}
3639
})
3740

3841
// Handle the post request from the upload_form.html to the server.

_examples/i18n/basic/main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,15 @@ func newApp() *iris.Application {
8181
app.RegisterView(view)
8282

8383
app.Get("/templates", func(ctx iris.Context) {
84-
ctx.View("index.html", iris.Map{
84+
if err := ctx.View("index.html", iris.Map{
8585
"tr": ctx.Tr, // word, arguments... {call .tr "hi" "iris"}}
8686
"trUnsafe": func(message string, args ...interface{}) template.HTML {
8787
return template.HTML(ctx.Tr(message, args...))
8888
},
89-
})
89+
}); err != nil {
90+
ctx.HTML("<h3>%s</h3>", err.Error())
91+
return
92+
}
9093

9194
// Note that,
9295
// Iris automatically adds a "tr" global template function as well,

_examples/mvc/error-handler-custom-result/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func (e errorResponse) Dispatch(ctx iris.Context) {
4949
// switch e.Code {
5050
// case iris.StatusNotFound:
5151
// // use Code and Message as the template data.
52-
// ctx.View("404.html", e)
52+
// if err := ctx.View("404.html", e)
5353
// default:
54-
// ctx.View("500.html", e)
54+
// if err := ctx.View("500.html", e)
5555
// }
5656
}
5757

_examples/mvc/login-mvc-single-responsibility/views/shared/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77

88
<body>
9-
{{ yield }}
9+
{{ yield . }}
1010
</body>
1111

1212
</html>

_examples/mvc/login/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ func main() {
3333
app.OnAnyErrorCode(func(ctx iris.Context) {
3434
ctx.ViewData("Message", ctx.Values().
3535
GetStringDefault("message", "The page you're looking for doesn't exist"))
36-
ctx.View("shared/error.html")
36+
if err := ctx.View("shared/error.html"); err != nil {
37+
ctx.HTML("<h3>%s</h3>", err.Error())
38+
return
39+
}
3740
})
3841

3942
// ---- Serve our controllers. ----

_examples/mvc/login/web/views/shared/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</head>
77

88
<body>
9-
{{ yield }}
9+
{{ yield . }}
1010
</body>
1111

1212
</html>

_examples/mvc/websocket-auth/main.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ func newApp() *iris.Application {
5151
}
5252

5353
func renderSigninForm(ctx iris.Context) {
54-
ctx.View("signin", iris.Map{"Title": "Signin Page"})
54+
if err := ctx.View("signin", iris.Map{"Title": "Signin Page"}); err != nil {
55+
ctx.HTML("<h3>%s</h3>", err.Error())
56+
return
57+
}
5558
}
5659

5760
type websocketController struct {

0 commit comments

Comments
 (0)