Skip to content

Commit

Permalink
builtin html template functions changes
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 12, 2022
1 parent abeae40 commit 1ea5cd5
Show file tree
Hide file tree
Showing 115 changed files with 472 additions and 230 deletions.
2 changes: 2 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The codebase for Dependency Injection, Internationalization and localization and

## Fixes and Improvements

- **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.

- 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.

- [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).
Expand Down
5 changes: 4 additions & 1 deletion _benchmarks/view/ace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ func index(ctx iris.Context) {
}

ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
2 changes: 1 addition & 1 deletion _benchmarks/view/ace/views/layouts/main.ace
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ html
head
title {{.Title}}
body
{{ yield }}
{{ yield . . }}
footer
= include partials/footer.ace .
5 changes: 4 additions & 1 deletion _benchmarks/view/amber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ func index(ctx iris.Context) {
// On Amber this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
5 changes: 4 additions & 1 deletion _benchmarks/view/blocks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ func index(ctx iris.Context) {
}

ctx.ViewLayout("main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
2 changes: 1 addition & 1 deletion _benchmarks/view/blocks/views/layouts/main.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ template "content" . }}<footer>{{ partial "partials/footer" .}}</footer></body></html>
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ template "content" . }}<footer>{{ partial "partials/footer" . }}</footer></body></html>
5 changes: 4 additions & 1 deletion _benchmarks/view/django/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ func index(ctx iris.Context) {
// On Django this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
5 changes: 4 additions & 1 deletion _benchmarks/view/handlebars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ func index(ctx iris.Context) {
}

ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
2 changes: 1 addition & 1 deletion _benchmarks/view/handlebars/views/layouts/main.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><title>{{Title}}</title></head><body>{{ yield }}<footer>{{ render "partials/footer.html" .}}</footer></body></html>
<!DOCTYPE html><html><head><title>{{Title}}</title></head><body>{{ yield . }}<footer>{{ render "partials/footer.html" .}}</footer></body></html>
5 changes: 4 additions & 1 deletion _benchmarks/view/html/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@ func index(ctx iris.Context) {
}

ctx.ViewLayout("layouts/main")
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
2 changes: 1 addition & 1 deletion _benchmarks/view/html/views/layouts/main.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ yield }}<footer>{{ render "partials/footer.html" }}</footer></body></html>
<!DOCTYPE html><html><head><title>{{.Title}}</title></head><body>{{ yield . }}<footer>{{ render "partials/footer.html" . }}</footer></body></html>
5 changes: 4 additions & 1 deletion _benchmarks/view/jet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ func index(ctx iris.Context) {
// On Jet this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
5 changes: 4 additions & 1 deletion _benchmarks/view/pug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ func index(ctx iris.Context) {
// On Pug this is ignored: ctx.ViewLayout("layouts/main")
// Layouts are only rendered from inside the index page itself
// using the "extends" keyword.
ctx.View("index", data)
if err := ctx.View("index", data); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
5 changes: 4 additions & 1 deletion _examples/auth/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ func main() {
}

func renderSigninForm(ctx iris.Context) {
ctx.View("signin", iris.Map{"Title": "Signin Page"})
if err := ctx.View("signin", iris.Map{"Title": "Signin Page"}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

func renderMemberPage(s *auth.Auth[User]) iris.Handler {
Expand Down
2 changes: 1 addition & 1 deletion _examples/auth/auth/views/layouts/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<body>
<div class="container">
<main>{{ template "content" . }}</main>
<footer style="position: fixed; bottom: 0; width: 100%;">{{ partial "partials/footer" .}}</footer>
<footer style="position: fixed; bottom: 0; width: 100%;">{{ partial "partials/footer" . }}</footer>
</div>
</body>
</html>
17 changes: 13 additions & 4 deletions _examples/auth/goth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,12 @@ func main() {
// ctx.View("user.html", user)
//
// Directly (user as .user variable):
ctx.View("user.html", iris.Map{
if err := ctx.View("user.html", iris.Map{
"user": user,
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

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

ctx.View("user.html", gothUser)
if err := ctx.View("user.html", gothUser); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

app.Get("/", func(ctx iris.Context) {
ctx.View("index.html", providerIndex)
if err := ctx.View("index.html", providerIndex); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

// http://localhost:3000
Expand Down
5 changes: 4 additions & 1 deletion _examples/auth/hcaptcha/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ func register(ctx iris.Context) {

func registerForm(ctx iris.Context) {
ctx.ViewData("SiteKey", siteKey)
ctx.View("register_form.html")
if err := ctx.View("register_form.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
5 changes: 4 additions & 1 deletion _examples/bootstrapper/bootstrap/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func (b *Bootstrapper) SetupErrorHandlers() {

ctx.ViewData("Err", err)
ctx.ViewData("Title", "Error")
ctx.View("shared/error.html")
if err := ctx.View("shared/error.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
}

Expand Down
5 changes: 4 additions & 1 deletion _examples/bootstrapper/routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ import (
// GetIndexHandler handles the GET: /
func GetIndexHandler(ctx iris.Context) {
ctx.ViewData("Title", "Index Page")
ctx.View("index.html")
if err := ctx.View("index.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
2 changes: 1 addition & 1 deletion _examples/bootstrapper/views/shared/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<body>
<div>
<!-- Render the current template here -->
{{ yield }}
{{ yield . }}
<hr />
<footer>
<p>&copy; 2017 - {{.AppOwner}}</p>
Expand Down
2 changes: 1 addition & 1 deletion _examples/caddy/server1/views/shared/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</head>

<body>
{{ yield }}
{{ yield . }}
</body>

</html>
5 changes: 4 additions & 1 deletion _examples/dropzonejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func main() {
// Render the actual form
// GET: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
ctx.View("upload.html")
if err := ctx.View("upload.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

// Upload the file to the server
Expand Down
5 changes: 4 additions & 1 deletion _examples/dropzonejs/README_PART2.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ func main() {
app.HandleDir("/public", iris.Dir("./public"))

app.Get("/", func(ctx iris.Context) {
ctx.View("upload.html")
if err := ctx.View("upload.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

files := scanUploads(uploadsDir)
Expand Down
5 changes: 4 additions & 1 deletion _examples/dropzonejs/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ func main() {
app.HandleDir("/public", iris.Dir("./public"))

app.Get("/", func(ctx iris.Context) {
ctx.View("upload.html")
if err := ctx.View("upload.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

files := scanUploads(uploadsDir)
Expand Down
5 changes: 4 additions & 1 deletion _examples/file-server/file-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func uploadView(ctx iris.Context) {
io.WriteString(h, strconv.FormatInt(now, 10))
token := fmt.Sprintf("%x", h.Sum(nil))

ctx.View("upload.html", token)
if err := ctx.View("upload.html", token); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

func upload(ctx iris.Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/x/errors"
)

// $ go install github.com/go-bindata/go-bindata/v3/go-bindata@latest
Expand All @@ -28,7 +27,7 @@ func newApp() *iris.Application {
app.Get("/", func(ctx iris.Context) {
ctx.ViewData("Page", page)
if err := ctx.View("index.html"); err != nil {
errors.InvalidArgument.Err(ctx, err)
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
Expand Down
5 changes: 4 additions & 1 deletion _examples/file-server/spa-vue-router/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func fullVueRouter() {
}
func index(ctx iris.Context) {
ctx.View("index.html")
if err := ctx.View("index.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}
*/
5 changes: 4 additions & 1 deletion _examples/file-server/upload-file/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func main() {
// ctx.ViewData("", token)
// or add second argument to the `View` method.
// Token will be passed as {{.}} in the template.
ctx.View("upload_form.html", token)
if err := ctx.View("upload_form.html", token); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

/* Read before continue.
Expand Down
5 changes: 4 additions & 1 deletion _examples/file-server/upload-files/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func newApp() *iris.Application {
token := fmt.Sprintf("%x", h.Sum(nil))

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

// Handle the post request from the upload_form.html to the server.
Expand Down
7 changes: 5 additions & 2 deletions _examples/i18n/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,15 @@ func newApp() *iris.Application {
app.RegisterView(view)

app.Get("/templates", func(ctx iris.Context) {
ctx.View("index.html", iris.Map{
if err := ctx.View("index.html", iris.Map{
"tr": ctx.Tr, // word, arguments... {call .tr "hi" "iris"}}
"trUnsafe": func(message string, args ...interface{}) template.HTML {
return template.HTML(ctx.Tr(message, args...))
},
})
}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}

// Note that,
// Iris automatically adds a "tr" global template function as well,
Expand Down
4 changes: 2 additions & 2 deletions _examples/mvc/error-handler-custom-result/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func (e errorResponse) Dispatch(ctx iris.Context) {
// switch e.Code {
// case iris.StatusNotFound:
// // use Code and Message as the template data.
// ctx.View("404.html", e)
// if err := ctx.View("404.html", e)
// default:
// ctx.View("500.html", e)
// if err := ctx.View("500.html", e)
// }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>

<body>
{{ yield }}
{{ yield . }}
</body>

</html>
5 changes: 4 additions & 1 deletion _examples/mvc/login/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func main() {
app.OnAnyErrorCode(func(ctx iris.Context) {
ctx.ViewData("Message", ctx.Values().
GetStringDefault("message", "The page you're looking for doesn't exist"))
ctx.View("shared/error.html")
if err := ctx.View("shared/error.html"); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})

// ---- Serve our controllers. ----
Expand Down
2 changes: 1 addition & 1 deletion _examples/mvc/login/web/views/shared/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</head>

<body>
{{ yield }}
{{ yield . }}
</body>

</html>
5 changes: 4 additions & 1 deletion _examples/mvc/websocket-auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func newApp() *iris.Application {
}

func renderSigninForm(ctx iris.Context) {
ctx.View("signin", iris.Map{"Title": "Signin Page"})
if err := ctx.View("signin", iris.Map{"Title": "Signin Page"}); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
}

type websocketController struct {
Expand Down
Loading

0 comments on commit 1ea5cd5

Please sign in to comment.