1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-07 12:07:28 +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)
}

View File

@@ -0,0 +1,3 @@
{{ define "content" }}
<h1>Hello, Admin!</h1>
{{ end }}

View File

@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
</head>
<body>
{{ template "content" .}}
<h4>Copyright &copy; 2020 Admin</h4>
</body>
</html>

View File

@@ -0,0 +1,2 @@
<h1>On-fly</h1>
<h3>{{.Message}}</h3>

View File

@@ -0,0 +1,12 @@
<!-- You can define more than one block.
The default one is "content" which should be the main template's body.
So, even if it's missing (see index.html), it's added automatically by the view engine.
When you need to define more than one block, you have to be more specific:
-->
{{ define "content" }}
<h1>Internal Server Error</h1>
{{ end }}
{{ define "message" }}
<p style="color:red;">{{.Message}}</p>
{{ end }}

View File

@@ -0,0 +1 @@
<h1>Index Body</h1>

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Code}}</title>
</head>
<body>
{{ template "content" .}}
{{block "message" .}}{{end}}
</body>
</html>

View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ if .Title }}{{ .Title }}{{ else }}Default Main Title{{ end }}</title>
</head>
<body>
{{ template "content" . }}
<footer>{{ partial "partials/footer" .}}</footer>
</body>
</html>

View File

@@ -0,0 +1 @@
<h3>Footer Partial</h3>