mirror of
https://github.com/kataras/iris.git
synced 2025-03-21 21:56:28 +01:00
minor: godoc
This commit is contained in:
parent
65e60df44e
commit
8b28ae14f8
|
@ -27,24 +27,35 @@ func main() {
|
||||||
|
|
||||||
app.Get("/example_map", func(ctx iris.Context) {
|
app.Get("/example_map", func(ctx iris.Context) {
|
||||||
ctx.View("example.html", iris.Map{
|
ctx.View("example.html", iris.Map{
|
||||||
"Name": "Example Name",
|
"Name": "Example Name",
|
||||||
"Age": 42,
|
"Age": 42,
|
||||||
"Items": []string{"Example slice entry 1", "entry 2", "entry 3"},
|
"Items": []string{"Example slice entry 1", "entry 2", "entry 3"},
|
||||||
"Map": iris.Map{"map key": "map value", "other key": "other value"},
|
"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) {
|
app.Get("/example_struct", func(ctx iris.Context) {
|
||||||
|
type book struct {
|
||||||
|
Title string
|
||||||
|
Pages int
|
||||||
|
}
|
||||||
|
|
||||||
var examplePage = struct {
|
var examplePage = struct {
|
||||||
Name string
|
Name string
|
||||||
Age int
|
Age int
|
||||||
Items []string
|
Items []string
|
||||||
Map map[string]interface{}
|
Map map[string]interface{}
|
||||||
|
Nested book
|
||||||
}{
|
}{
|
||||||
"Example Name",
|
"Example Name",
|
||||||
42,
|
42,
|
||||||
[]string{"Example slice entry 1", "entry 2", "entry 3"},
|
[]string{"Example slice entry 1", "entry 2", "entry 3"},
|
||||||
iris.Map{"map key": "map value", "other key": "other value"},
|
iris.Map{"map key": "map value", "other key": "other value"},
|
||||||
|
book{
|
||||||
|
"Iris E-Book",
|
||||||
|
620,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.View("example.html", examplePage)
|
ctx.View("example.html", examplePage)
|
||||||
|
|
|
@ -6,18 +6,23 @@
|
||||||
<title>HTML Template Example</title>
|
<title>HTML Template Example</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2>{{.Name}}</h2>
|
<h2>Name: {{.Name}}</h2>
|
||||||
<h3>{{.Age}}</h3>
|
<h3>Age: {{.Age}}</h3>
|
||||||
|
|
||||||
<h4>Slice</h4>
|
<h4>Slice</h4>
|
||||||
{{range $idx, $item := .Items}}
|
{{range $idx, $item := .Items}}
|
||||||
{{ $idx }} - {{ $item }} <br/>
|
{{ $idx }} - {{ $item }} <br/>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<h4>Map</h4>
|
<h4>Map</h4>
|
||||||
{{range $key, $value := .Map}}
|
{{range $key, $value := .Map}}
|
||||||
{{ $key }} - {{ $value }} <br/>
|
{{ $key }} - {{ $value }} <br/>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
<h4>Nested</h4>
|
||||||
|
|
||||||
|
<strong>Title:</strong>{{ .Nested.Title}}
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
|
|
||||||
<strong>Read more at: <a href="https://golang.org/pkg/html/template" target="_new">
|
<strong>Read more at: <a href="https://golang.org/pkg/html/template" target="_new">
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/kataras/iris/v12"
|
import (
|
||||||
|
"github.com/kataras/iris/v12"
|
||||||
|
"github.com/kataras/iris/v12/mvc"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := iris.New()
|
app := iris.New()
|
||||||
|
@ -28,8 +31,9 @@ func main() {
|
||||||
ctx.View("example.html", viewData)
|
ctx.View("example.html", viewData)
|
||||||
})
|
})
|
||||||
|
|
||||||
/* See context-view-data example: Set data through one or more middleware
|
exampleRouter := app.Party("/example")
|
||||||
app.Get("/view_data", func(ctx iris.Context) {
|
/* 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("author", map[string]string{"firstName": "Jean", "lastName": "Valjean"})
|
||||||
ctx.ViewData("body", "Life is difficult")
|
ctx.ViewData("body", "Life is difficult")
|
||||||
ctx.ViewData("comments", []iris.Map{{
|
ctx.ViewData("comments", []iris.Map{{
|
||||||
|
@ -37,14 +41,35 @@ func main() {
|
||||||
"body": "LOL!",
|
"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()
|
ctx.Next()
|
||||||
}, func(ctx iris.Context) {
|
|
||||||
ctx.View("example.html")
|
|
||||||
})
|
})
|
||||||
*/
|
|
||||||
|
mvc.New(exampleRouter).Handle(new(controller))
|
||||||
|
|
||||||
// Read more about its syntax at:
|
// Read more about its syntax at:
|
||||||
// https://github.com/aymerick/raymond and
|
// https://github.com/aymerick/raymond and
|
||||||
// https://handlebarsjs.com/guide
|
// https://handlebarsjs.com/guide
|
||||||
|
|
||||||
|
// http://localhost:8080
|
||||||
|
// http://localhost:8080/example
|
||||||
app.Listen(":8080")
|
app.Listen(":8080")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type controller struct{}
|
||||||
|
|
||||||
|
func (c *controller) Get() mvc.Result {
|
||||||
|
return mvc.View{
|
||||||
|
Name: "example",
|
||||||
|
Code: 200,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ import (
|
||||||
"github.com/fatih/structs"
|
"github.com/fatih/structs"
|
||||||
"github.com/iris-contrib/schema"
|
"github.com/iris-contrib/schema"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/kataras/golog"
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"github.com/russross/blackfriday/v2"
|
"github.com/russross/blackfriday/v2"
|
||||||
"github.com/vmihailenco/msgpack/v5"
|
"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"
|
// 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.
|
// then you pass the "users/index.html" as the filename argument.
|
||||||
//
|
//
|
||||||
// The second optional argument can receive a single "view model"
|
// The second optional argument can receive a single "view model".
|
||||||
// that will be binded to the view template if it's not nil,
|
// If "optionalViewModel" exists, even if it's nil, overrides any previous `ViewData` calls.
|
||||||
// otherwise it will check for previous view data stored by the `ViewData`
|
// If second argument is missing then binds the data through previous `ViewData` calls (e.g. middleware).
|
||||||
// even if stored at any previous handler(middleware) for the same request.
|
|
||||||
//
|
//
|
||||||
// Look .ViewData and .ViewLayout too.
|
// 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.
|
err := ctx.app.View(ctx, filename, layout, bindingData) // if failed it logs the error.
|
||||||
if err != nil {
|
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
|
return err
|
||||||
|
|
Loading…
Reference in New Issue
Block a user