From 1ea5cd58be284ad90e15289c6517db84654f3cc8 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Tue, 13 Dec 2022 01:37:15 +0200 Subject: [PATCH] builtin html template functions changes --- HISTORY.md | 2 + _benchmarks/view/ace/main.go | 5 +- _benchmarks/view/ace/views/layouts/main.ace | 2 +- _benchmarks/view/amber/main.go | 5 +- _benchmarks/view/blocks/main.go | 5 +- .../view/blocks/views/layouts/main.html | 2 +- _benchmarks/view/django/main.go | 5 +- _benchmarks/view/handlebars/main.go | 5 +- .../view/handlebars/views/layouts/main.html | 2 +- _benchmarks/view/html/main.go | 5 +- _benchmarks/view/html/views/layouts/main.html | 2 +- _benchmarks/view/jet/main.go | 5 +- _benchmarks/view/pug/main.go | 5 +- _examples/auth/auth/main.go | 5 +- _examples/auth/auth/views/layouts/main.html | 2 +- _examples/auth/goth/main.go | 17 +++-- _examples/auth/hcaptcha/main.go | 5 +- .../bootstrapper/bootstrap/bootstrapper.go | 5 +- _examples/bootstrapper/routes/index.go | 5 +- .../bootstrapper/views/shared/layout.html | 2 +- .../caddy/server1/views/shared/layout.html | 2 +- _examples/dropzonejs/README.md | 5 +- _examples/dropzonejs/README_PART2.md | 5 +- _examples/dropzonejs/src/main.go | 5 +- _examples/file-server/file-server/main.go | 5 +- .../embedded-single-page-application/main.go | 3 +- _examples/file-server/spa-vue-router/main.go | 5 +- _examples/file-server/upload-file/main.go | 5 +- _examples/file-server/upload-files/main.go | 5 +- _examples/i18n/basic/main.go | 7 +- .../mvc/error-handler-custom-result/main.go | 4 +- .../views/shared/layout.html | 2 +- _examples/mvc/login/main.go | 5 +- .../mvc/login/web/views/shared/layout.html | 2 +- _examples/mvc/websocket-auth/main.go | 5 +- .../websocket-auth/views/layouts/main.html | 2 +- .../request-body/read-form/checkboxes/main.go | 5 +- _examples/request-body/read-form/main.go | 2 +- _examples/routing/overview-2/main.go | 10 ++- .../subdomains/http-errors-view/main.go | 7 +- .../http-errors-view/views/error.html | 4 +- .../views/layouts/layout.html | 2 +- .../views/layouts/test.layout.html | 2 +- _examples/routing/versioning/main.go | 5 +- _examples/sessions/viewdata/main.go | 19 ++--- _examples/url-shortener/main.go | 5 +- .../templates/layouts/layout.html | 2 +- _examples/view/context-view-engine/main.go | 15 +++- .../views/public/layouts/main.html | 2 +- .../main.go | 10 ++- .../templates/layouts/layout.html | 2 +- .../templates/layouts/mylayout.html | 2 +- .../templates/page1.html | 2 +- .../embedded/templates/layouts/layout.html | 2 +- .../embedded/templates/layouts/mylayout.html | 2 +- .../embedded/templates/page1.html | 2 +- .../view/embedding-templates-into-app/main.go | 15 ++-- _examples/view/fallback/main.go | 5 +- _examples/view/layout/ace/main.go | 5 +- .../view/layout/ace/views/layouts/main.ace | 2 +- _examples/view/layout/amber/main.go | 5 +- _examples/view/layout/blocks/main.go | 5 +- .../layout/blocks/views/layouts/main.html | 2 +- _examples/view/layout/django/main.go | 5 +- _examples/view/layout/handlebars/main.go | 5 +- .../layout/handlebars/views/layouts/main.html | 2 +- _examples/view/layout/html/main.go | 5 +- .../view/layout/html/views/layouts/main.html | 4 +- _examples/view/layout/jet/main.go | 5 +- .../view/layout/jet/views/layouts/main.jet | 2 +- _examples/view/layout/pug/main.go | 5 +- _examples/view/overview/main.go | 25 ++++--- _examples/view/parse-template/amber/main.go | 7 +- _examples/view/parse-template/django/main.go | 7 +- .../view/parse-template/handlebars/main.go | 7 +- _examples/view/parse-template/jet/main.go | 7 +- _examples/view/parse-template/main.go | 7 +- .../parse-template/views/layouts/main.html | 2 +- _examples/view/template_ace_0/main.go | 16 +++-- .../template_ace_0/views/layouts/main.ace | 2 +- _examples/view/template_amber_0/main.go | 7 +- .../view/template_amber_1_embedded/main.go | 7 +- _examples/view/template_blocks_0/main.go | 10 ++- .../template_blocks_0/views/layouts/main.html | 2 +- .../view/template_blocks_1_embedded/main.go | 10 ++- _examples/view/template_django_0/main.go | 7 +- _examples/view/template_django_1/main.go | 3 +- _examples/view/template_handlebars_0/main.go | 5 +- _examples/view/template_html_0/main.go | 13 ++-- _examples/view/template_html_1/main.go | 5 +- .../template_html_1/templates/layout.html | 2 +- _examples/view/template_html_2/main.go | 10 ++- .../templates/layouts/layout.html | 2 +- .../templates/layouts/mylayout.html | 2 +- .../view/template_html_2/templates/page1.html | 2 +- _examples/view/template_html_3/main.go | 3 +- _examples/view/template_html_4/main.go | 3 +- _examples/view/template_html_5/main.go | 16 ++++- .../view/template_html_5/views/layout.html | 6 +- _examples/view/template_jet_0/main.go | 10 ++- .../view/template_jet_1_embedded/main.go | 5 +- _examples/view/template_jet_2/main.go | 3 +- _examples/view/template_jet_3/main.go | 5 +- _examples/view/template_pug_0/main.go | 5 +- _examples/view/template_pug_1/main.go | 5 +- .../view/template_pug_2_embedded/main.go | 5 +- _examples/websocket/gorilla-filewatch/main.go | 5 +- _examples/websocket/native-messages/main.go | 5 +- _examples/websocket/online-visitors/main.go | 10 ++- core/router/api_builder.go | 9 ++- core/router/party.go | 5 +- view/README.md | 23 ++++-- view/ace.go | 2 +- view/handlebars.go | 6 +- view/html.go | 72 ++++++------------- 115 files changed, 472 insertions(+), 230 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0e74149992..fe4c3436ae 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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). diff --git a/_benchmarks/view/ace/main.go b/_benchmarks/view/ace/main.go index b5f32ea65d..4bd1bf10c4 100644 --- a/_benchmarks/view/ace/main.go +++ b/_benchmarks/view/ace/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/ace/views/layouts/main.ace b/_benchmarks/view/ace/views/layouts/main.ace index d90a58fe78..b1e74baafc 100644 --- a/_benchmarks/view/ace/views/layouts/main.ace +++ b/_benchmarks/view/ace/views/layouts/main.ace @@ -3,6 +3,6 @@ html head title {{.Title}} body - {{ yield }} + {{ yield . . }} footer = include partials/footer.ace . diff --git a/_benchmarks/view/amber/main.go b/_benchmarks/view/amber/main.go index 742fd668ac..4b63f6842c 100644 --- a/_benchmarks/view/amber/main.go +++ b/_benchmarks/view/amber/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/blocks/main.go b/_benchmarks/view/blocks/main.go index 5a9957eda3..62b86b4c02 100644 --- a/_benchmarks/view/blocks/main.go +++ b/_benchmarks/view/blocks/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/blocks/views/layouts/main.html b/_benchmarks/view/blocks/views/layouts/main.html index 934080b2f4..7686db9083 100644 --- a/_benchmarks/view/blocks/views/layouts/main.html +++ b/_benchmarks/view/blocks/views/layouts/main.html @@ -1 +1 @@ -{{.Title}}{{ template "content" . }} \ No newline at end of file +{{.Title}}{{ template "content" . }} \ No newline at end of file diff --git a/_benchmarks/view/django/main.go b/_benchmarks/view/django/main.go index 4c625c492b..ad6cc0341d 100644 --- a/_benchmarks/view/django/main.go +++ b/_benchmarks/view/django/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/handlebars/main.go b/_benchmarks/view/handlebars/main.go index 0cd5c7015a..70daefe59d 100644 --- a/_benchmarks/view/handlebars/main.go +++ b/_benchmarks/view/handlebars/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/handlebars/views/layouts/main.html b/_benchmarks/view/handlebars/views/layouts/main.html index 50a79f87e4..c37a0de865 100644 --- a/_benchmarks/view/handlebars/views/layouts/main.html +++ b/_benchmarks/view/handlebars/views/layouts/main.html @@ -1 +1 @@ -{{Title}}{{ yield }} \ No newline at end of file +{{Title}}{{ yield . }} \ No newline at end of file diff --git a/_benchmarks/view/html/main.go b/_benchmarks/view/html/main.go index 214c4b1122..494f1c76cd 100644 --- a/_benchmarks/view/html/main.go +++ b/_benchmarks/view/html/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/html/views/layouts/main.html b/_benchmarks/view/html/views/layouts/main.html index 379a5ffad6..398a0aadb4 100644 --- a/_benchmarks/view/html/views/layouts/main.html +++ b/_benchmarks/view/html/views/layouts/main.html @@ -1 +1 @@ -{{.Title}}{{ yield }} \ No newline at end of file +{{.Title}}{{ yield . }} \ No newline at end of file diff --git a/_benchmarks/view/jet/main.go b/_benchmarks/view/jet/main.go index c588875971..aa28cdf65f 100644 --- a/_benchmarks/view/jet/main.go +++ b/_benchmarks/view/jet/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_benchmarks/view/pug/main.go b/_benchmarks/view/pug/main.go index fd4e340e60..aa1affcaca 100644 --- a/_benchmarks/view/pug/main.go +++ b/_benchmarks/view/pug/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/auth/auth/main.go b/_examples/auth/auth/main.go index eca0be59d3..4858eee16a 100644 --- a/_examples/auth/auth/main.go +++ b/_examples/auth/auth/main.go @@ -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("

%s

", err.Error()) + return + } } func renderMemberPage(s *auth.Auth[User]) iris.Handler { diff --git a/_examples/auth/auth/views/layouts/main.html b/_examples/auth/auth/views/layouts/main.html index c6efd56d0f..b44c3648c3 100644 --- a/_examples/auth/auth/views/layouts/main.html +++ b/_examples/auth/auth/views/layouts/main.html @@ -24,7 +24,7 @@
{{ template "content" . }}
-
{{ partial "partials/footer" .}}
+
{{ partial "partials/footer" . }}
\ No newline at end of file diff --git a/_examples/auth/goth/main.go b/_examples/auth/goth/main.go index 9fc4f4b10a..0a188cfb6d 100644 --- a/_examples/auth/goth/main.go +++ b/_examples/auth/goth/main.go @@ -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("

%s

", err.Error()) + return + } }) app.Get("/logout/{provider}", func(ctx iris.Context) { @@ -395,11 +398,17 @@ func main() { return } - ctx.View("user.html", gothUser) + if err := ctx.View("user.html", gothUser); err != nil { + ctx.HTML("

%s

", 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("

%s

", err.Error()) + return + } }) // http://localhost:3000 diff --git a/_examples/auth/hcaptcha/main.go b/_examples/auth/hcaptcha/main.go index 87e917acbc..7d4e929850 100644 --- a/_examples/auth/hcaptcha/main.go +++ b/_examples/auth/hcaptcha/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/bootstrapper/bootstrap/bootstrapper.go b/_examples/bootstrapper/bootstrap/bootstrapper.go index 0d24c50cc1..4b2ddbf2b9 100644 --- a/_examples/bootstrapper/bootstrap/bootstrapper.go +++ b/_examples/bootstrapper/bootstrap/bootstrapper.go @@ -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("

%s

", err.Error()) + return + } }) } diff --git a/_examples/bootstrapper/routes/index.go b/_examples/bootstrapper/routes/index.go index 4ce214d483..5a3140449a 100644 --- a/_examples/bootstrapper/routes/index.go +++ b/_examples/bootstrapper/routes/index.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/bootstrapper/views/shared/layout.html b/_examples/bootstrapper/views/shared/layout.html index cf92044434..70891776a6 100644 --- a/_examples/bootstrapper/views/shared/layout.html +++ b/_examples/bootstrapper/views/shared/layout.html @@ -12,7 +12,7 @@
- {{ yield }} + {{ yield . }}

© 2017 - {{.AppOwner}}

diff --git a/_examples/caddy/server1/views/shared/layout.html b/_examples/caddy/server1/views/shared/layout.html index 141a75f75c..3854c7fdd1 100644 --- a/_examples/caddy/server1/views/shared/layout.html +++ b/_examples/caddy/server1/views/shared/layout.html @@ -5,7 +5,7 @@ - {{ yield }} + {{ yield . }} \ No newline at end of file diff --git a/_examples/dropzonejs/README.md b/_examples/dropzonejs/README.md index d2ec00fc8f..0e6c6ac9c8 100644 --- a/_examples/dropzonejs/README.md +++ b/_examples/dropzonejs/README.md @@ -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("

%s

", err.Error()) + return + } }) // Upload the file to the server diff --git a/_examples/dropzonejs/README_PART2.md b/_examples/dropzonejs/README_PART2.md index e120e32aa4..39c68178bc 100644 --- a/_examples/dropzonejs/README_PART2.md +++ b/_examples/dropzonejs/README_PART2.md @@ -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("

%s

", err.Error()) + return + } }) files := scanUploads(uploadsDir) diff --git a/_examples/dropzonejs/src/main.go b/_examples/dropzonejs/src/main.go index edc97f8597..56edf24ca2 100644 --- a/_examples/dropzonejs/src/main.go +++ b/_examples/dropzonejs/src/main.go @@ -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("

%s

", err.Error()) + return + } }) files := scanUploads(uploadsDir) diff --git a/_examples/file-server/file-server/main.go b/_examples/file-server/file-server/main.go index 73d74d4472..a49f73e1ac 100644 --- a/_examples/file-server/file-server/main.go +++ b/_examples/file-server/file-server/main.go @@ -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("

%s

", err.Error()) + return + } } func upload(ctx iris.Context) { diff --git a/_examples/file-server/single-page-application/embedded-single-page-application/main.go b/_examples/file-server/single-page-application/embedded-single-page-application/main.go index 0834fd7846..3e0f65d7cd 100644 --- a/_examples/file-server/single-page-application/embedded-single-page-application/main.go +++ b/_examples/file-server/single-page-application/embedded-single-page-application/main.go @@ -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 @@ -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("

%s

", err.Error()) return } }) diff --git a/_examples/file-server/spa-vue-router/main.go b/_examples/file-server/spa-vue-router/main.go index 5018288a6b..680f2be224 100644 --- a/_examples/file-server/spa-vue-router/main.go +++ b/_examples/file-server/spa-vue-router/main.go @@ -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("

%s

", err.Error()) + return + } } */ diff --git a/_examples/file-server/upload-file/main.go b/_examples/file-server/upload-file/main.go index ef4a48072e..ccec044ed9 100644 --- a/_examples/file-server/upload-file/main.go +++ b/_examples/file-server/upload-file/main.go @@ -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("

%s

", err.Error()) + return + } }) /* Read before continue. diff --git a/_examples/file-server/upload-files/main.go b/_examples/file-server/upload-files/main.go index f2cd9673a2..c992a167cc 100644 --- a/_examples/file-server/upload-files/main.go +++ b/_examples/file-server/upload-files/main.go @@ -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("

%s

", err.Error()) + return + } }) // Handle the post request from the upload_form.html to the server. diff --git a/_examples/i18n/basic/main.go b/_examples/i18n/basic/main.go index 02b80a6075..6bdd77c7ed 100644 --- a/_examples/i18n/basic/main.go +++ b/_examples/i18n/basic/main.go @@ -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("

%s

", err.Error()) + return + } // Note that, // Iris automatically adds a "tr" global template function as well, diff --git a/_examples/mvc/error-handler-custom-result/main.go b/_examples/mvc/error-handler-custom-result/main.go index c9805c8e10..e7e2618b7f 100644 --- a/_examples/mvc/error-handler-custom-result/main.go +++ b/_examples/mvc/error-handler-custom-result/main.go @@ -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) // } } diff --git a/_examples/mvc/login-mvc-single-responsibility/views/shared/layout.html b/_examples/mvc/login-mvc-single-responsibility/views/shared/layout.html index 1e02677b07..d6b50dcf24 100644 --- a/_examples/mvc/login-mvc-single-responsibility/views/shared/layout.html +++ b/_examples/mvc/login-mvc-single-responsibility/views/shared/layout.html @@ -6,7 +6,7 @@ - {{ yield }} + {{ yield . }} \ No newline at end of file diff --git a/_examples/mvc/login/main.go b/_examples/mvc/login/main.go index 26f8f4aefb..ebda0367a0 100644 --- a/_examples/mvc/login/main.go +++ b/_examples/mvc/login/main.go @@ -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("

%s

", err.Error()) + return + } }) // ---- Serve our controllers. ---- diff --git a/_examples/mvc/login/web/views/shared/layout.html b/_examples/mvc/login/web/views/shared/layout.html index 75e14fb05c..2fce6a0899 100644 --- a/_examples/mvc/login/web/views/shared/layout.html +++ b/_examples/mvc/login/web/views/shared/layout.html @@ -6,7 +6,7 @@ - {{ yield }} + {{ yield . }} \ No newline at end of file diff --git a/_examples/mvc/websocket-auth/main.go b/_examples/mvc/websocket-auth/main.go index eaea5da546..cec1f2ea08 100644 --- a/_examples/mvc/websocket-auth/main.go +++ b/_examples/mvc/websocket-auth/main.go @@ -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("

%s

", err.Error()) + return + } } type websocketController struct { diff --git a/_examples/mvc/websocket-auth/views/layouts/main.html b/_examples/mvc/websocket-auth/views/layouts/main.html index c6efd56d0f..b44c3648c3 100644 --- a/_examples/mvc/websocket-auth/views/layouts/main.html +++ b/_examples/mvc/websocket-auth/views/layouts/main.html @@ -24,7 +24,7 @@
{{ template "content" . }}
-
{{ partial "partials/footer" .}}
+
{{ partial "partials/footer" . }}
\ No newline at end of file diff --git a/_examples/request-body/read-form/checkboxes/main.go b/_examples/request-body/read-form/checkboxes/main.go index bd9994dc5d..4007fbf03a 100644 --- a/_examples/request-body/read-form/checkboxes/main.go +++ b/_examples/request-body/read-form/checkboxes/main.go @@ -13,7 +13,10 @@ func main() { } func showForm(ctx iris.Context) { - ctx.View("form.html") + if err := ctx.View("form.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } type formExample struct { diff --git a/_examples/request-body/read-form/main.go b/_examples/request-body/read-form/main.go index cb82ffe487..942a23785c 100644 --- a/_examples/request-body/read-form/main.go +++ b/_examples/request-body/read-form/main.go @@ -19,7 +19,7 @@ func main() { app.Get("/", func(ctx iris.Context) { if err := ctx.View("form.html"); err != nil { - ctx.StopWithError(iris.StatusInternalServerError, err) + ctx.HTML("

%s

", err.Error()) return } }) diff --git a/_examples/routing/overview-2/main.go b/_examples/routing/overview-2/main.go index 83b222cf40..c4b2e29b10 100644 --- a/_examples/routing/overview-2/main.go +++ b/_examples/routing/overview-2/main.go @@ -100,7 +100,10 @@ func profileByUsername(ctx iris.Context) { ctx.ViewData("Username", username) // renders "./views/user/profile.html" // with {{ .Username }} equals to the username dynamic path parameter. - ctx.View("user/profile.html") + if err := ctx.View("user/profile.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func getUserByID(ctx iris.Context) { @@ -122,5 +125,8 @@ func createUser(ctx iris.Context) { // renders "./views/user/create_verification.html" // with {{ . }} equals to the User object, i.e {{ .Username }} , {{ .Firstname}} etc... ctx.ViewData("", user) - ctx.View("user/create_verification.html") + if err := ctx.View("user/create_verification.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/routing/subdomains/http-errors-view/main.go b/_examples/routing/subdomains/http-errors-view/main.go index 3b8ec6fcbd..34fe93e405 100644 --- a/_examples/routing/subdomains/http-errors-view/main.go +++ b/_examples/routing/subdomains/http-errors-view/main.go @@ -28,9 +28,12 @@ func newApp() *iris.Application { } func handleNotFoundTestSubdomain(ctx iris.Context) { - ctx.View("error.html", iris.Map{ + if err := ctx.View("error.html", iris.Map{ "ErrorCode": ctx.GetStatusCode(), - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func testIndex(ctx iris.Context) { diff --git a/_examples/routing/subdomains/http-errors-view/views/error.html b/_examples/routing/subdomains/http-errors-view/views/error.html index fb015a9544..bad88d5b11 100644 --- a/_examples/routing/subdomains/http-errors-view/views/error.html +++ b/_examples/routing/subdomains/http-errors-view/views/error.html @@ -2,8 +2,8 @@

Oups, you've got an error!

{{ if .ErrorCode }} {{ $tmplName := print "partials/" .ErrorCode ".html"}} - {{ render $tmplName }} + {{ render $tmplName . }} {{ else }} - {{ render "partials/500.html" }} + {{ render "partials/500.html" . }} {{ end }}
diff --git a/_examples/routing/subdomains/http-errors-view/views/layouts/layout.html b/_examples/routing/subdomains/http-errors-view/views/layouts/layout.html index e454f1fca0..cd6fa3d5e7 100644 --- a/_examples/routing/subdomains/http-errors-view/views/layouts/layout.html +++ b/_examples/routing/subdomains/http-errors-view/views/layouts/layout.html @@ -7,6 +7,6 @@

This is the global layout


- {{ yield }} + {{ yield . }} diff --git a/_examples/routing/subdomains/http-errors-view/views/layouts/test.layout.html b/_examples/routing/subdomains/http-errors-view/views/layouts/test.layout.html index 8849bd756a..957cee9532 100644 --- a/_examples/routing/subdomains/http-errors-view/views/layouts/test.layout.html +++ b/_examples/routing/subdomains/http-errors-view/views/layouts/test.layout.html @@ -5,6 +5,6 @@ - {{ yield }} + {{ yield . }} diff --git a/_examples/routing/versioning/main.go b/_examples/routing/versioning/main.go index 3e18aa7e7d..052a87a340 100644 --- a/_examples/routing/versioning/main.go +++ b/_examples/routing/versioning/main.go @@ -98,5 +98,8 @@ func testError(v string) iris.Handler { } func testView(ctx iris.Context) { - ctx.View("index.html") + if err := ctx.View("index.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/sessions/viewdata/main.go b/_examples/sessions/viewdata/main.go index 44aae9c1eb..e20424b07b 100644 --- a/_examples/sessions/viewdata/main.go +++ b/_examples/sessions/viewdata/main.go @@ -29,14 +29,17 @@ func setSessionViewData(ctx iris.Context) { func index(ctx iris.Context) { session := sessions.Get(ctx) session.Set("username", "kataras") - ctx.View("index") + if err := ctx.View("index"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } /* OR without middleware: - ctx.View("index", iris.Map{ - "session": session, - // {{.session.Get "username"}} - // OR to pass only the 'username': - // "username": session.Get("username"), - // {{.username}} - }) + if err := ctx.View("index", iris.Map{ + "session": session, + // {{.session.Get "username"}} + // OR to pass only the 'username': + // "username": session.Get("username"), + // {{.username}} + }) */ } diff --git a/_examples/url-shortener/main.go b/_examples/url-shortener/main.go index 8ce91c1821..ac10db8df8 100644 --- a/_examples/url-shortener/main.go +++ b/_examples/url-shortener/main.go @@ -50,7 +50,10 @@ func newApp(db *DB) *iris.Application { indexHandler := func(ctx iris.Context) { ctx.ViewData("URL_COUNT", db.Len()) - ctx.View("index.html") + if err := ctx.View("index.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } app.Get("/", indexHandler) diff --git a/_examples/view/context-view-data/templates/layouts/layout.html b/_examples/view/context-view-data/templates/layouts/layout.html index a483377482..4e0179c7bf 100644 --- a/_examples/view/context-view-data/templates/layouts/layout.html +++ b/_examples/view/context-view-data/templates/layouts/layout.html @@ -5,6 +5,6 @@ - {{ yield }} + {{ yield . . }} diff --git a/_examples/view/context-view-engine/main.go b/_examples/view/context-view-engine/main.go index ddc6d90f1e..22ee15efcb 100644 --- a/_examples/view/context-view-engine/main.go +++ b/_examples/view/context-view-engine/main.go @@ -37,7 +37,10 @@ func index(ctx iris.Context) { } ctx.ViewLayout("main") - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func admin(ctx iris.Context) { @@ -46,7 +49,10 @@ func admin(ctx iris.Context) { } ctx.ViewLayout("main") - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func setViews(views iris.ViewEngine) iris.Handler { @@ -61,5 +67,8 @@ func onFly(ctx iris.Context) { "Message": "View engine changed through 'setViews' custom middleware.", } - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/context-view-engine/views/public/layouts/main.html b/_examples/view/context-view-engine/views/public/layouts/main.html index 5b5a509f4c..e2b112b32b 100644 --- a/_examples/view/context-view-engine/views/public/layouts/main.html +++ b/_examples/view/context-view-engine/views/public/layouts/main.html @@ -8,6 +8,6 @@ {{ template "content" . }} - + \ No newline at end of file diff --git a/_examples/view/embedding-templates-into-app-bindata/main.go b/_examples/view/embedding-templates-into-app-bindata/main.go index ffd8368359..fbecfddaae 100644 --- a/_examples/view/embedding-templates-into-app-bindata/main.go +++ b/_examples/view/embedding-templates-into-app-bindata/main.go @@ -37,10 +37,16 @@ func main() { my := app.Party("/my").Layout("layouts/mylayout.html") { // both of these will use the layouts/mylayout.html as their layout. my.Get("/", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) my.Get("/other", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) } diff --git a/_examples/view/embedding-templates-into-app-bindata/templates/layouts/layout.html b/_examples/view/embedding-templates-into-app-bindata/templates/layouts/layout.html index 69b545ece1..929a31cd8e 100644 --- a/_examples/view/embedding-templates-into-app-bindata/templates/layouts/layout.html +++ b/_examples/view/embedding-templates-into-app-bindata/templates/layouts/layout.html @@ -7,6 +7,6 @@

This is the global layout


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/embedding-templates-into-app-bindata/templates/layouts/mylayout.html b/_examples/view/embedding-templates-into-app-bindata/templates/layouts/mylayout.html index d22426feb5..4cbced1837 100644 --- a/_examples/view/embedding-templates-into-app-bindata/templates/layouts/mylayout.html +++ b/_examples/view/embedding-templates-into-app-bindata/templates/layouts/mylayout.html @@ -7,6 +7,6 @@

This is the layout for the /my/ and /my/other routes only


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/embedding-templates-into-app-bindata/templates/page1.html b/_examples/view/embedding-templates-into-app-bindata/templates/page1.html index 6f63f7b30b..03659b45cc 100644 --- a/_examples/view/embedding-templates-into-app-bindata/templates/page1.html +++ b/_examples/view/embedding-templates-into-app-bindata/templates/page1.html @@ -2,6 +2,6 @@

Page 1 {{ greet "iris developer"}}

- {{ render "partials/page1_partial1.html"}} + {{ render "partials/page1_partial1.html" . }} diff --git a/_examples/view/embedding-templates-into-app/embedded/templates/layouts/layout.html b/_examples/view/embedding-templates-into-app/embedded/templates/layouts/layout.html index 69b545ece1..929a31cd8e 100644 --- a/_examples/view/embedding-templates-into-app/embedded/templates/layouts/layout.html +++ b/_examples/view/embedding-templates-into-app/embedded/templates/layouts/layout.html @@ -7,6 +7,6 @@

This is the global layout


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/embedding-templates-into-app/embedded/templates/layouts/mylayout.html b/_examples/view/embedding-templates-into-app/embedded/templates/layouts/mylayout.html index d22426feb5..4cbced1837 100644 --- a/_examples/view/embedding-templates-into-app/embedded/templates/layouts/mylayout.html +++ b/_examples/view/embedding-templates-into-app/embedded/templates/layouts/mylayout.html @@ -7,6 +7,6 @@

This is the layout for the /my/ and /my/other routes only


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/embedding-templates-into-app/embedded/templates/page1.html b/_examples/view/embedding-templates-into-app/embedded/templates/page1.html index 6f63f7b30b..03659b45cc 100644 --- a/_examples/view/embedding-templates-into-app/embedded/templates/page1.html +++ b/_examples/view/embedding-templates-into-app/embedded/templates/page1.html @@ -2,6 +2,6 @@

Page 1 {{ greet "iris developer"}}

- {{ render "partials/page1_partial1.html"}} + {{ render "partials/page1_partial1.html" . }} diff --git a/_examples/view/embedding-templates-into-app/main.go b/_examples/view/embedding-templates-into-app/main.go index 2fc45ed6d0..b6f5d2caa8 100644 --- a/_examples/view/embedding-templates-into-app/main.go +++ b/_examples/view/embedding-templates-into-app/main.go @@ -4,7 +4,6 @@ import ( "embed" "github.com/kataras/iris/v12" - "github.com/kataras/iris/v12/x/errors" ) //go:embed embedded/* @@ -24,7 +23,7 @@ func main() { app.Get("/", func(ctx iris.Context) { if err := ctx.View("page1.html"); err != nil { - errors.InvalidArgument.Err(ctx, err) + ctx.HTML("

%s

", err.Error()) return } }) @@ -33,7 +32,7 @@ func main() { app.Get("/nolayout", func(ctx iris.Context) { ctx.ViewLayout(iris.NoLayout) if err := ctx.View("page1.html"); err != nil { - errors.InvalidArgument.Err(ctx, err) + ctx.HTML("

%s

", err.Error()) return } }) @@ -42,10 +41,16 @@ func main() { my := app.Party("/my").Layout("layouts/mylayout.html") { // both of these will use the layouts/mylayout.html as their layout. my.Get("/", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) my.Get("/other", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) } diff --git a/_examples/view/fallback/main.go b/_examples/view/fallback/main.go index 4783786675..067744b759 100644 --- a/_examples/view/fallback/main.go +++ b/_examples/view/fallback/main.go @@ -38,5 +38,8 @@ func main() { // } func index(ctx iris.Context) { - ctx.View("blabla.html") + if err := ctx.View("blabla.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/ace/main.go b/_examples/view/layout/ace/main.go index a73cebe502..e3d05eefc5 100644 --- a/_examples/view/layout/ace/main.go +++ b/_examples/view/layout/ace/main.go @@ -22,5 +22,8 @@ func index(ctx iris.Context) { } ctx.ViewLayout("layouts/main") - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/ace/views/layouts/main.ace b/_examples/view/layout/ace/views/layouts/main.ace index 010cb9fe81..d17414b359 100644 --- a/_examples/view/layout/ace/views/layouts/main.ace +++ b/_examples/view/layout/ace/views/layouts/main.ace @@ -3,6 +3,6 @@ html head title {{.Title}} body - {{ yield }} + {{ yield . }} footer = include partials/footer.ace . \ No newline at end of file diff --git a/_examples/view/layout/amber/main.go b/_examples/view/layout/amber/main.go index c0b95b3bd7..92195ee3fe 100644 --- a/_examples/view/layout/amber/main.go +++ b/_examples/view/layout/amber/main.go @@ -21,5 +21,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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/blocks/main.go b/_examples/view/layout/blocks/main.go index 5a9957eda3..62b86b4c02 100644 --- a/_examples/view/layout/blocks/main.go +++ b/_examples/view/layout/blocks/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/blocks/views/layouts/main.html b/_examples/view/layout/blocks/views/layouts/main.html index e033deefe3..e8bd6089d9 100644 --- a/_examples/view/layout/blocks/views/layouts/main.html +++ b/_examples/view/layout/blocks/views/layouts/main.html @@ -5,7 +5,7 @@ {{ template "content" . }} \ No newline at end of file diff --git a/_examples/view/layout/django/main.go b/_examples/view/layout/django/main.go index 4c625c492b..ad6cc0341d 100644 --- a/_examples/view/layout/django/main.go +++ b/_examples/view/layout/django/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/handlebars/main.go b/_examples/view/layout/handlebars/main.go index 8d30373d9f..7b62c7b8fd 100644 --- a/_examples/view/layout/handlebars/main.go +++ b/_examples/view/layout/handlebars/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/handlebars/views/layouts/main.html b/_examples/view/layout/handlebars/views/layouts/main.html index dcb986204d..1dd581b8ae 100644 --- a/_examples/view/layout/handlebars/views/layouts/main.html +++ b/_examples/view/layout/handlebars/views/layouts/main.html @@ -3,7 +3,7 @@ {{Title}} - {{ yield }} + {{ yield . }} diff --git a/_examples/view/layout/html/main.go b/_examples/view/layout/html/main.go index 214c4b1122..494f1c76cd 100644 --- a/_examples/view/layout/html/main.go +++ b/_examples/view/layout/html/main.go @@ -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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/html/views/layouts/main.html b/_examples/view/layout/html/views/layouts/main.html index fe646117eb..898f7f6039 100644 --- a/_examples/view/layout/html/views/layouts/main.html +++ b/_examples/view/layout/html/views/layouts/main.html @@ -3,9 +3,9 @@ {{.Title}} -{{ yield }} +{{ yield . }} \ No newline at end of file diff --git a/_examples/view/layout/jet/main.go b/_examples/view/layout/jet/main.go index 503503966b..075266b3e0 100644 --- a/_examples/view/layout/jet/main.go +++ b/_examples/view/layout/jet/main.go @@ -22,5 +22,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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/layout/jet/views/layouts/main.jet b/_examples/view/layout/jet/views/layouts/main.jet index e896d1e043..39a625cd98 100644 --- a/_examples/view/layout/jet/views/layouts/main.jet +++ b/_examples/view/layout/jet/views/layouts/main.jet @@ -3,7 +3,7 @@ {{.Title}} - {{ yield documentBody() }} + {{ yield . documentBody() }} \ No newline at end of file diff --git a/_examples/view/layout/pug/main.go b/_examples/view/layout/pug/main.go index bb43e07dc6..4432545fa2 100644 --- a/_examples/view/layout/pug/main.go +++ b/_examples/view/layout/pug/main.go @@ -21,5 +21,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("

%s

", err.Error()) + return + } } diff --git a/_examples/view/overview/main.go b/_examples/view/overview/main.go index 6f6387b0e7..d75e275901 100644 --- a/_examples/view/overview/main.go +++ b/_examples/view/overview/main.go @@ -8,10 +8,10 @@ func main() { // with default template funcs: // // - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }} - // - {{ render "header.html" }} - // - {{ render_r "header.html" }} // partial relative path to current page - // - {{ yield }} - // - {{ current }} + // - {{ render "header.html" . }} + // - {{ render_r "header.html" . }} // partial relative path to current page + // - {{ yield . }} + // - {{ current . }} app.RegisterView(iris.HTML("./templates", ".html"). Reload(true)) // Set Reload false to production. @@ -23,17 +23,23 @@ func main() { ctx.ViewData("Name", "iris") // render the template with the file name relative to the './templates'. // file extension is OPTIONAL. - ctx.View("hi.html") + if err := ctx.View("hi.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Get("/example_map", func(ctx iris.Context) { - ctx.View("example.html", iris.Map{ + if err := ctx.View("example.html", iris.Map{ "Name": "Example Name", "Age": 42, "Items": []string{"Example slice entry 1", "entry 2", "entry 3"}, "Map": iris.Map{"map key": "map value", "other key": "other value"}, "Nested": iris.Map{"Title": "Iris E-Book", "Pages": 620}, - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Get("/example_struct", func(ctx iris.Context) { @@ -59,7 +65,10 @@ func main() { }, } - ctx.View("example.html", examplePage) + if err := ctx.View("example.html", examplePage); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) // http://localhost:8080/ diff --git a/_examples/view/parse-template/amber/main.go b/_examples/view/parse-template/amber/main.go index 422c28e580..3fa5829d7f 100644 --- a/_examples/view/parse-template/amber/main.go +++ b/_examples/view/parse-template/amber/main.go @@ -21,9 +21,12 @@ func main() { } func index(ctx iris.Context) { - ctx.View("program.amber", iris.Map{ + if err := ctx.View("program.amber", iris.Map{ "Name": "Gerasimos", // Or per template: // "greet": func(....) - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/parse-template/django/main.go b/_examples/view/parse-template/django/main.go index 8cf8ad6fd3..8fe6c08a3d 100644 --- a/_examples/view/parse-template/django/main.go +++ b/_examples/view/parse-template/django/main.go @@ -21,7 +21,10 @@ func main() { } func index(ctx iris.Context) { - ctx.View("program.html", iris.Map{ + if err := ctx.View("program.html", iris.Map{ "Name": "Gerasimos", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/parse-template/handlebars/main.go b/_examples/view/parse-template/handlebars/main.go index 76927270e9..1a6adb09ff 100644 --- a/_examples/view/parse-template/handlebars/main.go +++ b/_examples/view/parse-template/handlebars/main.go @@ -19,7 +19,10 @@ func main() { } func index(ctx iris.Context) { - ctx.View("program.html", iris.Map{ + if err := ctx.View("program.html", iris.Map{ "Name": "Gerasimos", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/parse-template/jet/main.go b/_examples/view/parse-template/jet/main.go index 565ad92930..9b07b32b9e 100644 --- a/_examples/view/parse-template/jet/main.go +++ b/_examples/view/parse-template/jet/main.go @@ -27,7 +27,10 @@ func main() { } func index(ctx iris.Context) { - ctx.View("program.jet", iris.Map{ + if err := ctx.View("program.jet", iris.Map{ "Name": "Gerasimos", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/parse-template/main.go b/_examples/view/parse-template/main.go index dd99e482ab..8c9bd88e1b 100644 --- a/_examples/view/parse-template/main.go +++ b/_examples/view/parse-template/main.go @@ -31,9 +31,12 @@ func main() { } func index(ctx iris.Context) { - ctx.View("program.html", iris.Map{ + if err := ctx.View("program.html", iris.Map{ "Name": "Gerasimos", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func layout(ctx iris.Context) { diff --git a/_examples/view/parse-template/views/layouts/main.html b/_examples/view/parse-template/views/layouts/main.html index 3f6b65a681..588c57bb4b 100644 --- a/_examples/view/parse-template/views/layouts/main.html +++ b/_examples/view/parse-template/views/layouts/main.html @@ -6,6 +6,6 @@

[layout] Body content is below...

- {{ yield }} + {{ yield . }} diff --git a/_examples/view/template_ace_0/main.go b/_examples/view/template_ace_0/main.go index a86a3c3f68..3e8637a0d7 100644 --- a/_examples/view/template_ace_0/main.go +++ b/_examples/view/template_ace_0/main.go @@ -12,16 +12,22 @@ func main() { app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { - ctx.View("index", iris.Map{ + if err := ctx.View("index", iris.Map{ "Title": "Title of The Page", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Get("/layout", func(ctx iris.Context) { - ctx.ViewLayout("layouts/main") // layout for that response. - ctx.View("index", iris.Map{ // file extension is optional. + ctx.ViewLayout("layouts/main") // layout for that response. + if err := ctx.View("index", iris.Map{ // file extension is optional. "Title": "Title of the main Page", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) // otherGroup := app.Party("/other").Layout("layouts/other.ace") -> layout for that party. diff --git a/_examples/view/template_ace_0/views/layouts/main.ace b/_examples/view/template_ace_0/views/layouts/main.ace index 5886865954..7e6e5ff3b9 100644 --- a/_examples/view/template_ace_0/views/layouts/main.ace +++ b/_examples/view/template_ace_0/views/layouts/main.ace @@ -4,4 +4,4 @@ html title Main Page body h1 Layout - {{ yield }} \ No newline at end of file + {{ yield . }} \ No newline at end of file diff --git a/_examples/view/template_amber_0/main.go b/_examples/view/template_amber_0/main.go index 8d868ed887..4d6155c0f6 100644 --- a/_examples/view/template_amber_0/main.go +++ b/_examples/view/template_amber_0/main.go @@ -11,9 +11,12 @@ func main() { app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { - ctx.View("index.amber", iris.Map{ + if err := ctx.View("index.amber", iris.Map{ "Title": "Title of The Page", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Listen(":8080") diff --git a/_examples/view/template_amber_1_embedded/main.go b/_examples/view/template_amber_1_embedded/main.go index 207c8e8810..36b8531549 100644 --- a/_examples/view/template_amber_1_embedded/main.go +++ b/_examples/view/template_amber_1_embedded/main.go @@ -19,9 +19,12 @@ func main() { app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { - ctx.View("index.amber", iris.Map{ + if err := ctx.View("index.amber", iris.Map{ "Title": "Title of The Page", - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Listen(":8080") diff --git a/_examples/view/template_blocks_0/main.go b/_examples/view/template_blocks_0/main.go index 8e46a50696..8171ffb336 100644 --- a/_examples/view/template_blocks_0/main.go +++ b/_examples/view/template_blocks_0/main.go @@ -19,7 +19,10 @@ func index(ctx iris.Context) { } ctx.ViewLayout("main") - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func internalServerError(ctx iris.Context) { @@ -31,5 +34,8 @@ func internalServerError(ctx iris.Context) { } ctx.ViewLayout("error") - ctx.View("500", data) + if err := ctx.View("500", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_blocks_0/views/layouts/main.html b/_examples/view/template_blocks_0/views/layouts/main.html index 5b5a509f4c..e2b112b32b 100644 --- a/_examples/view/template_blocks_0/views/layouts/main.html +++ b/_examples/view/template_blocks_0/views/layouts/main.html @@ -8,6 +8,6 @@ {{ template "content" . }} - + \ No newline at end of file diff --git a/_examples/view/template_blocks_1_embedded/main.go b/_examples/view/template_blocks_1_embedded/main.go index b3e772b6cb..f036cc810f 100644 --- a/_examples/view/template_blocks_1_embedded/main.go +++ b/_examples/view/template_blocks_1_embedded/main.go @@ -28,7 +28,10 @@ func index(ctx iris.Context) { } ctx.ViewLayout("main") - ctx.View("index", data) + if err := ctx.View("index", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } func internalServerError(ctx iris.Context) { @@ -40,5 +43,8 @@ func internalServerError(ctx iris.Context) { } ctx.ViewLayout("error") - ctx.View("500", data) + if err := ctx.View("500", data); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_django_0/main.go b/_examples/view/template_django_0/main.go index f18a97ece9..91104bbe30 100644 --- a/_examples/view/template_django_0/main.go +++ b/_examples/view/template_django_0/main.go @@ -35,9 +35,12 @@ func hi(ctx iris.Context) { // or if you set all view data in the same handler you can use the // iris.Map/pongo2.Context/map[string]interface{}, look below: - ctx.View("hi.html", iris.Map{ + if err := ctx.View("hi.html", iris.Map{ "title": "Hi Page", "name": "iris", "serverStartTime": startTime, - }) + }); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_django_1/main.go b/_examples/view/template_django_1/main.go index 5769cd9794..cb816352ec 100644 --- a/_examples/view/template_django_1/main.go +++ b/_examples/view/template_django_1/main.go @@ -31,7 +31,8 @@ func main() { paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"} ctx.ViewData("ParamsAsArray", paramsAsArray) if err := ctx.View("page.html"); err != nil { - panic(err) + ctx.HTML("

%s

", err.Error()) + return } }) diff --git a/_examples/view/template_handlebars_0/main.go b/_examples/view/template_handlebars_0/main.go index 77c61757e6..dac5417dd8 100644 --- a/_examples/view/template_handlebars_0/main.go +++ b/_examples/view/template_handlebars_0/main.go @@ -28,7 +28,10 @@ func main() { }}, } - ctx.View("example.html", viewData) + if err := ctx.View("example.html", viewData); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) exampleRouter := app.Party("/example") diff --git a/_examples/view/template_html_0/main.go b/_examples/view/template_html_0/main.go index 71ef18fcac..687adfb7e5 100644 --- a/_examples/view/template_html_0/main.go +++ b/_examples/view/template_html_0/main.go @@ -12,10 +12,10 @@ func main() { // default template funcs are: // // - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }} - // - {{ render "header.html" }} - // - {{ render_r "header.html" }} // partial relative path to current page - // - {{ yield }} - // - {{ current }} + // - {{ render "header.html" . }} + // - {{ render_r "header.html" . }} // partial relative path to current page + // - {{ yield . }} + // - {{ current . }} tmpl.AddFunc("greet", func(s string) string { return "Greetings " + s + "!" }) @@ -31,5 +31,8 @@ func hi(ctx iris.Context) { ctx.ViewData("Title", "Hi Page") ctx.ViewData("Name", "iris") // {{.Name}} will render: iris // ctx.ViewData("", myCcustomStruct{}) - ctx.View("hi.html") + if err := ctx.View("hi.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_html_1/main.go b/_examples/view/template_html_1/main.go index a2a3b45a3d..26d2e9a59c 100644 --- a/_examples/view/template_html_1/main.go +++ b/_examples/view/template_html_1/main.go @@ -18,7 +18,10 @@ func main() { app.Get("/", func(ctx iris.Context) { ctx.CompressWriter(true) ctx.ViewData("", mypage{"My Page title", "Hello world!"}) - ctx.View("mypage.html") + if err := ctx.View("mypage.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } // 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 diff --git a/_examples/view/template_html_1/templates/layout.html b/_examples/view/template_html_1/templates/layout.html index 96f0c7532f..7b18f46bb7 100644 --- a/_examples/view/template_html_1/templates/layout.html +++ b/_examples/view/template_html_1/templates/layout.html @@ -6,6 +6,6 @@

[layout] Body content is below...

- {{ yield }} + {{ yield . }} diff --git a/_examples/view/template_html_2/main.go b/_examples/view/template_html_2/main.go index 270b99ba58..6e864223ea 100644 --- a/_examples/view/template_html_2/main.go +++ b/_examples/view/template_html_2/main.go @@ -35,10 +35,16 @@ func main() { my := app.Party("/my").Layout("layouts/mylayout.html") { // both of these will use the layouts/mylayout.html as their layout. my.Get("/", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) my.Get("/other", func(ctx iris.Context) { - ctx.View("page1.html") + if err := ctx.View("page1.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) } diff --git a/_examples/view/template_html_2/templates/layouts/layout.html b/_examples/view/template_html_2/templates/layouts/layout.html index eb543f0ed8..731c213810 100644 --- a/_examples/view/template_html_2/templates/layouts/layout.html +++ b/_examples/view/template_html_2/templates/layouts/layout.html @@ -7,6 +7,6 @@

This is the global layout


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/template_html_2/templates/layouts/mylayout.html b/_examples/view/template_html_2/templates/layouts/mylayout.html index d87575d3b9..7dba0f336f 100644 --- a/_examples/view/template_html_2/templates/layouts/mylayout.html +++ b/_examples/view/template_html_2/templates/layouts/mylayout.html @@ -7,6 +7,6 @@

This is the layout for the /my/ and /my/other routes only


- {{ yield }} + {{ yield . }} diff --git a/_examples/view/template_html_2/templates/page1.html b/_examples/view/template_html_2/templates/page1.html index 8c1d8ed68c..75bbb430a0 100644 --- a/_examples/view/template_html_2/templates/page1.html +++ b/_examples/view/template_html_2/templates/page1.html @@ -2,6 +2,6 @@

Page 1 {{ greet "iris developer"}}

- {{ render "partials/page1_partial1.html" }} + {{ render "partials/page1_partial1.html" . }} diff --git a/_examples/view/template_html_3/main.go b/_examples/view/template_html_3/main.go index 8e02d3a783..91cc7e7500 100644 --- a/_examples/view/template_html_3/main.go +++ b/_examples/view/template_html_3/main.go @@ -35,7 +35,8 @@ func main() { paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"} ctx.ViewData("ParamsAsArray", paramsAsArray) if err := ctx.View("page.html"); err != nil { - panic(err) + ctx.HTML("

%s

", err.Error()) + return } }) diff --git a/_examples/view/template_html_4/main.go b/_examples/view/template_html_4/main.go index 9c0e4b2bbf..6a0ea14ea6 100644 --- a/_examples/view/template_html_4/main.go +++ b/_examples/view/template_html_4/main.go @@ -52,7 +52,8 @@ func main() { paramsAsArray := []string{"username5", "theParam1", "theParam2", "paramThirdAfterStatic"} ctx.ViewData("ParamsAsArray", paramsAsArray) if err := ctx.View("page.html"); err != nil { - panic(err) + ctx.HTML("

%s

", err.Error()) + return } }) diff --git a/_examples/view/template_html_5/main.go b/_examples/view/template_html_5/main.go index 7e79e0b236..3727c07da0 100644 --- a/_examples/view/template_html_5/main.go +++ b/_examples/view/template_html_5/main.go @@ -12,18 +12,28 @@ func main() { app.Get("/home", func(ctx iris.Context) { ctx.ViewData("title", "Home page") - ctx.View("home.html") + if err := ctx.View("home.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } + // 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") + if err := ctx.View("about.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Get("/user/index", func(ctx iris.Context) { - ctx.View("user/index.html") + if err := ctx.View("user/index.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) // http://localhost:8080 diff --git a/_examples/view/template_html_5/views/layout.html b/_examples/view/template_html_5/views/layout.html index c374205f7e..6fbb4c89c5 100644 --- a/_examples/view/template_html_5/views/layout.html +++ b/_examples/view/template_html_5/views/layout.html @@ -1,11 +1,11 @@ -{{ part "head" }} +{{ part "head"}}

[layout] Body content is below...

- {{ part "body" }} + {{ part "body" . }} - {{ yield }} + {{ yield . }} diff --git a/_examples/view/template_jet_0/main.go b/_examples/view/template_jet_0/main.go index 1ab0af1913..272b9a1e19 100644 --- a/_examples/view/template_jet_0/main.go +++ b/_examples/view/template_jet_0/main.go @@ -113,7 +113,10 @@ func main() { } ctx.ViewData("title", "Show TODO") - ctx.View("todos/show.jet", todo) + if err := ctx.View("todos/show.jet", todo); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Get("/all-done", func(ctx iris.Context) { // vars := make(view.JetRuntimeVars) @@ -132,7 +135,10 @@ func main() { // ctx.ViewData("_jet", (&doneTODOs{}).New(todos)) // and ctx.View("todos/index.jet") // OR - ctx.View("todos/index.jet", (&doneTODOs{}).New(todos)) + if err := ctx.View("todos/index.jet", (&doneTODOs{}).New(todos)); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) port := os.Getenv("PORT") diff --git a/_examples/view/template_jet_1_embedded/main.go b/_examples/view/template_jet_1_embedded/main.go index 28083ec56d..b6595078cd 100644 --- a/_examples/view/template_jet_1_embedded/main.go +++ b/_examples/view/template_jet_1_embedded/main.go @@ -22,7 +22,10 @@ func main() { app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { - ctx.View("index.jet") + if err := ctx.View("index.jet"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) addr := ":8080" diff --git a/_examples/view/template_jet_2/main.go b/_examples/view/template_jet_2/main.go index db872d2be5..d40bf4e57c 100644 --- a/_examples/view/template_jet_2/main.go +++ b/_examples/view/template_jet_2/main.go @@ -34,7 +34,8 @@ func main() { paramsAsArray := []string{"theParam1", "theParam2", "paramThirdAfterStatic"} ctx.ViewData("ParamsAsArray", paramsAsArray) if err := ctx.View("page.jet"); err != nil { - panic(err) + ctx.HTML("

%s

", err.Error()) + return } }) diff --git a/_examples/view/template_jet_3/main.go b/_examples/view/template_jet_3/main.go index 0ff91f6861..8d7cc86404 100644 --- a/_examples/view/template_jet_3/main.go +++ b/_examples/view/template_jet_3/main.go @@ -24,7 +24,10 @@ func main() { app.RegisterView(tmpl) app.Get("/", func(ctx iris.Context) { - ctx.View("index.jet") + if err := ctx.View("index.jet"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) app.Listen(":8080") diff --git a/_examples/view/template_pug_0/main.go b/_examples/view/template_pug_0/main.go index 27c50f06e9..548f2ac145 100644 --- a/_examples/view/template_pug_0/main.go +++ b/_examples/view/template_pug_0/main.go @@ -38,5 +38,8 @@ func index(ctx iris.Context) { Jobs: []*Job{&job1, &job2}, } - ctx.View("index.pug", person) + if err := ctx.View("index.pug", person); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_pug_1/main.go b/_examples/view/template_pug_1/main.go index 28186b14c7..fac8bb86dd 100644 --- a/_examples/view/template_pug_1/main.go +++ b/_examples/view/template_pug_1/main.go @@ -24,5 +24,8 @@ func main() { } func index(ctx iris.Context) { - ctx.View("index.pug") + if err := ctx.View("index.pug"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/view/template_pug_2_embedded/main.go b/_examples/view/template_pug_2_embedded/main.go index 50ee2b50df..c04d08ba9f 100644 --- a/_examples/view/template_pug_2_embedded/main.go +++ b/_examples/view/template_pug_2_embedded/main.go @@ -22,5 +22,8 @@ func main() { } func index(ctx iris.Context) { - ctx.View("index.pug") + if err := ctx.View("index.pug"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } diff --git a/_examples/websocket/gorilla-filewatch/main.go b/_examples/websocket/gorilla-filewatch/main.go index 00583eb39c..4254ab5041 100644 --- a/_examples/websocket/gorilla-filewatch/main.go +++ b/_examples/websocket/gorilla-filewatch/main.go @@ -146,7 +146,10 @@ func serveHome(ctx iris.Context) { string(p), strconv.FormatInt(lastMod.UnixNano(), 16), } - ctx.View("home.html", v) + if err := ctx.View("home.html", v); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } // $ go get github.com/gorilla/websocket diff --git a/_examples/websocket/native-messages/main.go b/_examples/websocket/native-messages/main.go index 19660642d1..ffd83d5737 100644 --- a/_examples/websocket/native-messages/main.go +++ b/_examples/websocket/native-messages/main.go @@ -49,7 +49,10 @@ func main() { app.Get("/my_endpoint", websocket.Handler(ws)) app.Get("/", func(ctx iris.Context) { - ctx.View("client.html", clientPage{"Client Page", "localhost:8080"}) + if err := ctx.View("client.html", clientPage{"Client Page", "localhost:8080"}); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) // Target some browser windows/tabs to http://localhost:8080 and send some messages, diff --git a/_examples/websocket/online-visitors/main.go b/_examples/websocket/online-visitors/main.go index 5df905b4f3..5b2b965144 100644 --- a/_examples/websocket/online-visitors/main.go +++ b/_examples/websocket/online-visitors/main.go @@ -33,12 +33,18 @@ func main() { h := func(ctx iris.Context) { ctx.ViewData("", page{PageID: "index page"}) - ctx.View("index.html") + if err := ctx.View("index.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } h2 := func(ctx iris.Context) { ctx.ViewData("", page{PageID: "other page"}) - ctx.View("other.html") + if err := ctx.View("other.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } // Open some browser tabs/or windows diff --git a/core/router/api_builder.go b/core/router/api_builder.go index e1dd8b646e..5ceac69bbd 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -1687,9 +1687,12 @@ func (api *APIBuilder) FallbackView(provider context.FallbackViewProvider) { // app.RegisterView(iris.$VIEW_ENGINE("./views", ".$extension")) // my := app.Party("/my").Layout("layouts/mylayout.html") // -// my.Get("/", func(ctx iris.Context) { -// ctx.View("page1.html") -// }) +// my.Get("/", func(ctx iris.Context) { +// if err := ctx.View("page1.html"); err != nil { +// ctx.HTML("

%s

", err.Error()) +// return +// } +// }) // // Examples: https://github.com/kataras/iris/tree/master/_examples/view func (api *APIBuilder) Layout(tmplLayoutFile string) Party { diff --git a/core/router/party.go b/core/router/party.go index ab49432e12..9720c5f256 100644 --- a/core/router/party.go +++ b/core/router/party.go @@ -452,7 +452,10 @@ type Party interface { // app.RegisterView(iris.$VIEW_ENGINE("./views", ".$extension")) // my := app.Party("/my").Layout("layouts/mylayout.html") // my.Get("/", func(ctx iris.Context) { - // ctx.View("page1.html") + // if err := ctx.View("page1.html"); err != nil { + // ctx.HTML("

%s

", err.Error()) + // return + // } // }) // // Examples: https://github.com/kataras/iris/tree/master/_examples/view diff --git a/view/README.md b/view/README.md index 3bc9e7cba2..a523296678 100644 --- a/view/README.md +++ b/view/README.md @@ -44,7 +44,10 @@ func main() { // Bind: {{.message}} with "Hello world!" ctx.ViewData("message", "Hello world!") // Render template file: ./views/hello.html - ctx.View("hello.html") + if err := ctx.View("hello.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } }) // Method: GET @@ -85,10 +88,10 @@ func main() { // builtin template funcs are: // // - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }} - // - {{ render "header.html" }} - // - {{ render_r "header.html" }} // partial relative path to current page - // - {{ yield }} - // - {{ current }} + // - {{ render "header.html" . }} + // - {{ render_r "header.html" . }} // partial relative path to current page + // - {{ yield . }} + // - {{ current . }} // register a custom template func. tmpl.AddFunc("greet", func(s string) string { @@ -106,7 +109,10 @@ func main() { func hi(ctx iris.Context) { // render the template file "./templates/hi.html" - ctx.View("hi.html") + if err := ctx.View("hi.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } ``` @@ -150,7 +156,10 @@ type page struct { func hi(ctx iris.Context) { // {{.Page.Title}} and {{Page.Name}} ctx.ViewData("Page", page{Title: "Hi Page", Name: "iris"}) - ctx.View("hi.html") + if err := ctx.View("hi.html"); err != nil { + ctx.HTML("

%s

", err.Error()) + return + } } ``` diff --git a/view/ace.go b/view/ace.go index e6cf90f972..04fe746f5c 100644 --- a/view/ace.go +++ b/view/ace.go @@ -45,7 +45,7 @@ func Ace(fs interface{}, extension string) *AceEngine { s.middleware = func(name string, text []byte) (contents string, err error) { once.Do(func() { // on first template parse, all funcs are given. - for k, v := range emptyFuncs { + for k, v := range s.getBuiltinFuncs(name) { funcs[k] = v } diff --git a/view/handlebars.go b/view/handlebars.go index 1a80c45a97..a5af28fd56 100644 --- a/view/handlebars.go +++ b/view/handlebars.go @@ -103,8 +103,8 @@ func (s *HandlebarsEngine) Reload(developmentMode bool) *HandlebarsEngine { } // Layout sets the layout template file which should use -// the {{ yield }} func to yield the main template file -// and optionally {{partial/partial_r/render}} to render +// the {{ yield . }} func to yield the main template file +// and optionally {{partial/partial_r/render . }} to render // other template files like headers and footers. func (s *HandlebarsEngine) Layout(layoutFile string) *HandlebarsEngine { s.layout = layoutFile @@ -238,7 +238,7 @@ func (s *HandlebarsEngine) ExecuteWriter(w io.Writer, filename string, layout st if context == nil { context = make(map[string]interface{}, 1) } - // I'm implemented the {{ yield }} as with the rest of template engines, so this is not inneed for iris, but the user can do that manually if want + // I'm implemented the {{ yield . }} as with the rest of template engines, so this is not inneed for iris, but the user can do that manually if want // there is no performance cost: raymond.RegisterPartialTemplate(name, tmpl) context["yield"] = raymond.SafeString(contents) } diff --git a/view/html.go b/view/html.go index 01b0eae607..5f31bd9c9b 100644 --- a/view/html.go +++ b/view/html.go @@ -53,27 +53,6 @@ var ( _ EngineFuncer = (*HTMLEngine)(nil) ) -var emptyFuncs = template.FuncMap{ - "yield": func(binding interface{}) (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") - }, - "partial_r": func() (string, error) { - return "", fmt.Errorf("block was called, yet no layout defined") - }, - "current": func() (string, error) { - return "", nil - }, - "render": func() (string, error) { - return "", nil - }, -} - // HTML creates and returns a new html view engine. // The html engine used like the "html/template" standard go package // but with a lot of extra features. @@ -175,8 +154,8 @@ func (s *HTMLEngine) Delims(left, right string) *HTMLEngine { } // Layout sets the layout template file which inside should use -// the {{ yield }} func to yield the main template file -// and optionally {{partial/partial_r/render}} to render other template files like headers and footers +// the {{ yield . }} func to yield the main template file +// and optionally {{partial/partial_r/render . }} to render other template files like headers and footers // // The 'tmplLayoutFile' is a relative path of the templates base directory, // for the template file with its extension. @@ -334,6 +313,7 @@ func (s *HTMLEngine) parseTemplate(name string, contents []byte, funcs template. name = strings.TrimPrefix(name, "/") tmpl := s.Templates.New(name) + // tmpl.Option("missingkey=error") tmpl.Option(s.options...) var text string @@ -347,7 +327,12 @@ func (s *HTMLEngine) parseTemplate(name string, contents []byte, funcs template. text = string(contents) } - tmpl.Funcs(emptyFuncs).Funcs(s.funcs) + tmpl.Funcs(s.getBuiltinFuncs(name)).Funcs(s.funcs) + + if strings.Contains(name, "layout") { + tmpl.Funcs(s.layoutFuncs) + } + if len(funcs) > 0 { tmpl.Funcs(funcs) // custom for this template. } @@ -376,27 +361,14 @@ func (s *HTMLEngine) executeTemplateBuf(name string, binding interface{}) (strin return result, err } -func (s *HTMLEngine) layoutFuncsFor(lt *template.Template, name string, binding interface{}) { - s.runtimeFuncsFor(lt, name, binding) - +func (s *HTMLEngine) getBuiltinFuncs(name string) template.FuncMap { funcs := template.FuncMap{ - "yield": func() (template.HTML, error) { + "yield": func(binding interface{}) (template.HTML, error) { result, err := s.executeTemplateBuf(name, binding) // Return safe HTML here since we are rendering our own template. return template.HTML(result), err }, - } - - for k, v := range s.layoutFuncs { - funcs[k] = v - } - - lt.Funcs(funcs) -} - -func (s *HTMLEngine) runtimeFuncsFor(t *template.Template, name string, binding interface{}) { - funcs := template.FuncMap{ - "part": func(partName string) (template.HTML, error) { + "part": func(partName string, binding interface{}) (template.HTML, error) { nameTemp := strings.ReplaceAll(name, s.extension, "") fullPartName := fmt.Sprintf("%s-%s", nameTemp, partName) result, err := s.executeTemplateBuf(fullPartName, binding) @@ -408,7 +380,7 @@ func (s *HTMLEngine) runtimeFuncsFor(t *template.Template, name string, binding "current": func() (string, error) { return name, nil }, - "partial": func(partialName string) (template.HTML, error) { + "partial": func(partialName string, binding interface{}) (template.HTML, error) { fullPartialName := fmt.Sprintf("%s-%s", partialName, name) if s.Templates.Lookup(fullPartialName) != nil { result, err := s.executeTemplateBuf(fullPartialName, binding) @@ -420,7 +392,7 @@ func (s *HTMLEngine) runtimeFuncsFor(t *template.Template, name string, binding // it would be easier for adding pages' style/script inline // for example when using partial_r '.script' in layout.html // templates/users/index.html would load templates/users/index.script.html - "partial_r": func(partialName string) (template.HTML, error) { + "partial_r": func(partialName string, binding interface{}) (template.HTML, error) { ext := filepath.Ext(name) root := name[:len(name)-len(ext)] fullPartialName := fmt.Sprintf("%s%s%s", root, partialName, ext) @@ -430,13 +402,13 @@ func (s *HTMLEngine) runtimeFuncsFor(t *template.Template, name string, binding } return "", nil }, - "render": func(fullPartialName string) (template.HTML, error) { + "render": func(fullPartialName string, binding interface{}) (template.HTML, error) { result, err := s.executeTemplateBuf(fullPartialName, binding) return template.HTML(result), err }, } - t.Funcs(funcs) + return funcs } // ExecuteWriter executes a template and writes its result to the w writer. @@ -455,21 +427,19 @@ func (s *HTMLEngine) ExecuteWriter(w io.Writer, name string, layout string, bind } } - t := s.Templates.Lookup(name) - if t == nil { - return ErrNotExist{Name: name, IsLayout: false, Data: bindingData} - } - s.runtimeFuncsFor(t, name, bindingData) - if layout = getLayout(layout, s.layout); layout != "" { lt := s.Templates.Lookup(layout) if lt == nil { return ErrNotExist{Name: layout, IsLayout: true, Data: bindingData} } - s.layoutFuncsFor(lt, name, bindingData) return lt.Execute(w, bindingData) } + t := s.Templates.Lookup(name) + if t == nil { + return ErrNotExist{Name: name, IsLayout: false, Data: bindingData} + } + return t.Execute(w, bindingData) }