minor: godoc

This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-08-30 17:18:04 +03:00
parent 65e60df44e
commit 8b28ae14f8
No known key found for this signature in database
GPG Key ID: 5DBE766BD26A54E7
4 changed files with 67 additions and 21 deletions

View File

@ -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)

View File

@ -6,18 +6,23 @@
<title>HTML Template Example</title>
</head>
<body>
<h2>{{.Name}}</h2>
<h3>{{.Age}}</h3>
<h2>Name: {{.Name}}</h2>
<h3>Age: {{.Age}}</h3>
<h4>Slice</h4>
{{range $idx, $item := .Items}}
{{ $idx }} - {{ $item }} <br/>
{{end}}
<h4>Map</h4>
{{range $key, $value := .Map}}
{{ $key }} - {{ $value }} <br/>
{{end}}
<h4>Nested</h4>
<strong>Title:</strong>{{ .Nested.Title}}
<hr/>
<strong>Read more at: <a href="https://golang.org/pkg/html/template" target="_new">

View File

@ -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,
}
}

View File

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