Skip to content

Commit 5d480dc

Browse files
committed
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
1 parent b363492 commit 5d480dc

File tree

22 files changed

+282
-66
lines changed

22 files changed

+282
-66
lines changed

HISTORY.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,8 @@ Response:
359359

360360
Other Improvements:
361361

362+
- Ability to register a view engine per group of routes or for the current chain of handlers through `Party.RegisterView` and `Context.ViewEngine` respectfully.
363+
362364
- Add [Blocks](_examples/view/template_blocks_0) template engine. <!-- Reminder for @kataras: follow https://github.com/flosch/pongo2/pull/236#issuecomment-668950566 discussion so we can get back on using the original pongo2 repository as they fixed the issue about an incompatible 3rd party package (although they need more fixes, that's why I commented there) -->
363365

364366
- Add [Ace](_examples/view/template_ace_0) template parser to the view engine and other minor improvements.
@@ -492,8 +494,9 @@ New Package-level Variables:
492494

493495
New Context Methods:
494496

495-
- `Context.SetErr(error)` and `Context.GetErr() error` helpers
496-
- `Context.CompressWriter(bool) error` and `Context.CompressReader(bool) error`
497+
- `Context.ViewEngine(ViewEngine)` to set a view engine on-fly for the current chain of handlers, responsible to render templates through `ctx.View`. [Example](_examples/view/context-view-engine).
498+
- `Context.SetErr(error)` and `Context.GetErr() error` helpers.
499+
- `Context.CompressWriter(bool) error` and `Context.CompressReader(bool) error`.
497500
- `Context.Clone() Context` returns a copy of the Context.
498501
- `Context.IsCanceled() bool` reports whether the request has been canceled by the client.
499502
- `Context.IsSSL() bool` reports whether the request is under HTTPS SSL (New `Configuration.SSLProxyHeaders` and `HostProxyHeaders` fields too).
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import "github.com/kataras/iris/v12"
4+
5+
func main() {
6+
app := iris.New()
7+
// Register a root view engine, as usual,
8+
// will be used to render files through Context.View method
9+
// when no Party or Handler-specific view engine is available.
10+
app.RegisterView(iris.Blocks("./views/public", ".html"))
11+
12+
// http://localhost:8080
13+
app.Get("/", index)
14+
15+
// Register a view engine per group of routes.
16+
adminGroup := app.Party("/admin")
17+
adminGroup.RegisterView(iris.Blocks("./views/admin", ".html"))
18+
19+
// http://localhost:8080/admin
20+
adminGroup.Get("/", admin)
21+
22+
// Register a view engine on-fly for the current chain of handlers.
23+
views := iris.Blocks("./views/on-fly", ".html")
24+
if err := views.Load(); err != nil {
25+
app.Logger().Fatal(err)
26+
}
27+
28+
// http://localhost:8080/on-fly
29+
app.Get("/on-fly", setViews(views), onFly)
30+
31+
app.Listen(":8080")
32+
}
33+
34+
func index(ctx iris.Context) {
35+
data := iris.Map{
36+
"Title": "Public Index Title",
37+
}
38+
39+
ctx.ViewLayout("main")
40+
ctx.View("index", data)
41+
}
42+
43+
func admin(ctx iris.Context) {
44+
data := iris.Map{
45+
"Title": "Admin Panel",
46+
}
47+
48+
ctx.ViewLayout("main")
49+
ctx.View("index", data)
50+
}
51+
52+
func setViews(views iris.ViewEngine) iris.Handler {
53+
return func(ctx iris.Context) {
54+
ctx.ViewEngine(views)
55+
ctx.Next()
56+
}
57+
}
58+
59+
func onFly(ctx iris.Context) {
60+
data := iris.Map{
61+
"Message": "View engine changed through 'setViews' custom middleware.",
62+
}
63+
64+
ctx.View("index", data)
65+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ define "content" }}
2+
<h1>Hello, Admin!</h1>
3+
{{ end }}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>{{ .Title }}</title>
7+
</head>
8+
<body>
9+
{{ template "content" .}}
10+
11+
12+
<h4>Copyright &copy; 2020 Admin</h4>
13+
</body>
14+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>On-fly</h1>
2+
<h3>{{.Message}}</h3>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!-- You can define more than one block.
2+
The default one is "content" which should be the main template's body.
3+
So, even if it's missing (see index.html), it's added automatically by the view engine.
4+
When you need to define more than one block, you have to be more specific:
5+
-->
6+
{{ define "content" }}
7+
<h1>Internal Server Error</h1>
8+
{{ end }}
9+
10+
{{ define "message" }}
11+
<p style="color:red;">{{.Message}}</p>
12+
{{ end }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1>Index Body</h1>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>{{.Code}}</title>
7+
</head>
8+
<body>
9+
{{ template "content" .}}
10+
11+
{{block "message" .}}{{end}}
12+
</body>
13+
</html>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>{{ if .Title }}{{ .Title }}{{ else }}Default Main Title{{ end }}</title>
7+
</head>
8+
<body>
9+
{{ template "content" . }}
10+
11+
<footer>{{ partial "partials/footer" .}}</footer>
12+
</body>
13+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h3>Footer Partial</h3>

0 commit comments

Comments
 (0)