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:
65
_examples/view/context-view-engine/main.go
Normal file
65
_examples/view/context-view-engine/main.go
Normal 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)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{{ define "content" }}
|
||||
<h1>Hello, Admin!</h1>
|
||||
{{ end }}
|
||||
@@ -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 © 2020 Admin</h4>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,2 @@
|
||||
<h1>On-fly</h1>
|
||||
<h3>{{.Message}}</h3>
|
||||
12
_examples/view/context-view-engine/views/public/500.html
Normal file
12
_examples/view/context-view-engine/views/public/500.html
Normal 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 }}
|
||||
@@ -0,0 +1 @@
|
||||
<h1>Index Body</h1>
|
||||
@@ -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>
|
||||
@@ -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>
|
||||
@@ -0,0 +1 @@
|
||||
<h3>Footer Partial</h3>
|
||||
Reference in New Issue
Block a user