diff --git a/_examples/view/overview/main.go b/_examples/view/overview/main.go index caef76a6..c8de338c 100644 --- a/_examples/view/overview/main.go +++ b/_examples/view/overview/main.go @@ -27,24 +27,35 @@ func main() { app.Get("/example_map", func(ctx iris.Context) { 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"}, + "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}, }) }) app.Get("/example_struct", func(ctx iris.Context) { + type book struct { + Title string + Pages int + } + var examplePage = struct { - Name string - Age int - Items []string - Map map[string]interface{} + Name string + Age int + Items []string + Map map[string]interface{} + Nested book }{ "Example Name", 42, []string{"Example slice entry 1", "entry 2", "entry 3"}, iris.Map{"map key": "map value", "other key": "other value"}, + book{ + "Iris E-Book", + 620, + }, } ctx.View("example.html", examplePage) diff --git a/_examples/view/overview/templates/example.html b/_examples/view/overview/templates/example.html index 53725068..d540a1d0 100644 --- a/_examples/view/overview/templates/example.html +++ b/_examples/view/overview/templates/example.html @@ -6,18 +6,23 @@ HTML Template Example -

{{.Name}}

-

{{.Age}}

+

Name: {{.Name}}

+

Age: {{.Age}}

Slice

{{range $idx, $item := .Items}} {{ $idx }} - {{ $item }}
{{end}} +

Map

{{range $key, $value := .Map}} {{ $key }} - {{ $value }}
{{end}} +

Nested

+ + Title:{{ .Nested.Title}} +
Read more at: diff --git a/_examples/view/template_handlebars_0/main.go b/_examples/view/template_handlebars_0/main.go index 0af2dc8f..4978bae5 100644 --- a/_examples/view/template_handlebars_0/main.go +++ b/_examples/view/template_handlebars_0/main.go @@ -1,6 +1,9 @@ package main -import "github.com/kataras/iris/v12" +import ( + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/mvc" +) func main() { app := iris.New() @@ -28,8 +31,9 @@ func main() { ctx.View("example.html", viewData) }) - /* See context-view-data example: Set data through one or more middleware - app.Get("/view_data", func(ctx iris.Context) { + exampleRouter := app.Party("/example") + /* See context-view-data example: Set data through one or more middleware */ + exampleRouter.Use(func(ctx iris.Context) { ctx.ViewData("author", map[string]string{"firstName": "Jean", "lastName": "Valjean"}) ctx.ViewData("body", "Life is difficult") ctx.ViewData("comments", []iris.Map{{ @@ -37,14 +41,35 @@ func main() { "body": "LOL!", }}) + // OR: + // ctx.ViewData("", iris.Map{ + // "author": map[string]string{"firstName": "Jean", "lastName": "Valjean"}, + // "body": "Life is difficult", + // "comments": []iris.Map{{ + // "author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"}, + // "body": "LOL!", + // }}, + // }) + ctx.Next() - }, func(ctx iris.Context) { - ctx.View("example.html") }) - */ + + mvc.New(exampleRouter).Handle(new(controller)) // Read more about its syntax at: // https://github.com/aymerick/raymond and // https://handlebarsjs.com/guide + + // http://localhost:8080 + // http://localhost:8080/example app.Listen(":8080") } + +type controller struct{} + +func (c *controller) Get() mvc.Result { + return mvc.View{ + Name: "example", + Code: 200, + } +} diff --git a/context/context.go b/context/context.go index 1ee2aa04..2b648a55 100644 --- a/context/context.go +++ b/context/context.go @@ -33,6 +33,7 @@ import ( "github.com/fatih/structs" "github.com/iris-contrib/schema" jsoniter "github.com/json-iterator/go" + "github.com/kataras/golog" "github.com/microcosm-cc/bluemonday" "github.com/russross/blackfriday/v2" "github.com/vmihailenco/msgpack/v5" @@ -2777,10 +2778,9 @@ func (ctx *Context) GetViewData() map[string]interface{} { // i.e: if directory is "./templates" and want to render the "./templates/users/index.html" // then you pass the "users/index.html" as the filename argument. // -// The second optional argument can receive a single "view model" -// that will be binded to the view template if it's not nil, -// otherwise it will check for previous view data stored by the `ViewData` -// even if stored at any previous handler(middleware) for the same request. +// The second optional argument can receive a single "view model". +// If "optionalViewModel" exists, even if it's nil, overrides any previous `ViewData` calls. +// If second argument is missing then binds the data through previous `ViewData` calls (e.g. middleware). // // Look .ViewData and .ViewLayout too. // @@ -2815,7 +2815,12 @@ func (ctx *Context) View(filename string, optionalViewModel ...interface{}) erro err := ctx.app.View(ctx, filename, layout, bindingData) // if failed it logs the error. if err != nil { - ctx.StopWithStatus(http.StatusInternalServerError) + if ctx.app.Logger().Level == golog.DebugLevel { + // send the error back to the client, when debug mode. + ctx.StopWithError(http.StatusInternalServerError, err) + } else { + ctx.StopWithStatus(http.StatusInternalServerError) + } } return err