mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 02:17:05 +00:00
Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded
Former-commit-id: da4f38eb9034daa49446df3ee529423b98f9b331
This commit is contained in:
56
_examples/view/context-view-data/main.go
Normal file
56
_examples/view/context-view-data/main.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/context"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultTitle = "My Awesome Site"
|
||||
DefaultLayout = "layouts/layout.html"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
// output startup banner and error logs on os.Stdout
|
||||
|
||||
// set the view engine target to ./templates folder
|
||||
app.RegisterView(iris.HTML("./templates", ".html").Reload(true))
|
||||
|
||||
app.Use(func(ctx context.Context) {
|
||||
// set the title, current time and a layout in order to be used if and when the next handler(s) calls the .Render function
|
||||
ctx.ViewData("Title", DefaultTitle)
|
||||
now := time.Now().Format(ctx.Application().ConfigurationReadOnly().GetTimeFormat())
|
||||
ctx.ViewData("CurrentTime", now)
|
||||
ctx.ViewLayout(DefaultLayout)
|
||||
|
||||
ctx.Next()
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx context.Context) {
|
||||
ctx.ViewData("BodyMessage", "a sample text here... setted by the route handler")
|
||||
if err := ctx.View("index.html"); err != nil {
|
||||
ctx.Application().Logger().Infof(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
app.Get("/about", func(ctx context.Context) {
|
||||
ctx.ViewData("Title", "My About Page")
|
||||
ctx.ViewData("BodyMessage", "about text here... setted by the route handler")
|
||||
|
||||
// same file, just to keep things simple.
|
||||
if err := ctx.View("index.html"); err != nil {
|
||||
ctx.Application().Logger().Infof(err.Error())
|
||||
}
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
// http://localhost:8080/about
|
||||
app.Run(iris.Addr(":8080"))
|
||||
}
|
||||
|
||||
// Notes: ViewData("", myCustomStruct{}) will set this myCustomStruct value as a root binding data,
|
||||
// so any View("other", "otherValue") will probably fail.
|
||||
// To clear the binding data: ctx.Set(ctx.Application().ConfigurationReadOnly().GetViewDataContextKey(), nil)
|
||||
Reference in New Issue
Block a user