1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

Ability to register a view engine per group of routes or for the current a chain of handlers

Example at: https://github.com/kataras/iris/tree/master/_examples/view/context-view-engine
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-05 19:34:55 +03:00
parent b363492cca
commit 5d480dc801
22 changed files with 282 additions and 66 deletions

View File

@@ -0,0 +1,65 @@
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// Register a root view engine, as usual,
// will be used to render files through Context.View method
// when no Party or Handler-specific view engine is available.
app.RegisterView(iris.Blocks("./views/public", ".html"))
// http://localhost:8080
app.Get("/", index)
// Register a view engine per group of routes.
adminGroup := app.Party("/admin")
adminGroup.RegisterView(iris.Blocks("./views/admin", ".html"))
// http://localhost:8080/admin
adminGroup.Get("/", admin)
// Register a view engine on-fly for the current chain of handlers.
views := iris.Blocks("./views/on-fly", ".html")
if err := views.Load(); err != nil {
app.Logger().Fatal(err)
}
// http://localhost:8080/on-fly
app.Get("/on-fly", setViews(views), onFly)
app.Listen(":8080")
}
func index(ctx iris.Context) {
data := iris.Map{
"Title": "Public Index Title",
}
ctx.ViewLayout("main")
ctx.View("index", data)
}
func admin(ctx iris.Context) {
data := iris.Map{
"Title": "Admin Panel",
}
ctx.ViewLayout("main")
ctx.View("index", data)
}
func setViews(views iris.ViewEngine) iris.Handler {
return func(ctx iris.Context) {
ctx.ViewEngine(views)
ctx.Next()
}
}
func onFly(ctx iris.Context) {
data := iris.Map{
"Message": "View engine changed through 'setViews' custom middleware.",
}
ctx.View("index", data)
}