Skip to content

Commit e7b4039

Browse files
committed
update Blocks module
1 parent e0ac28c commit e7b4039

File tree

16 files changed

+198
-105
lines changed

16 files changed

+198
-105
lines changed

_examples/view/layout/blocks/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ func main() {
88
// Note, in Blocks engine, layouts
99
// are used by their base names, the
1010
// blocks.LayoutDir(layoutDir) defaults to "./layouts".
11+
// .Blocks(...).Layout("main") for default layout for all views, it can be modified through ctx.ViewLayout though.
1112

1213
app.Get("/", index)
1314

_examples/view/template_blocks_0/views/500.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
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-
-->
61
{{ define "content" }}
72
<h1>Internal Server Error</h1>
83
{{ end }}

_examples/view/template_blocks_0/views/layouts/error.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<title>{{.Code}}</title>
77
</head>
88
<body>
9-
{{ template "content" .}}
9+
{{ template "content" . }}
1010

11-
{{block "message" .}}{{end}}
11+
{{ block "message" . }}Default Error Message{{ end }}
1212
</body>
1313
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "github.com/kataras/iris/v12"
4+
5+
// Based on https://github.com/kataras/iris/issues/2214.
6+
func main() {
7+
app := initApp()
8+
app.Listen(":8080")
9+
}
10+
11+
func initApp() *iris.Application {
12+
app := iris.New()
13+
app.Logger().SetLevel("debug")
14+
15+
tmpl := iris.Blocks("./src/public/html", ".html")
16+
tmpl.Layout("main")
17+
app.RegisterView(tmpl)
18+
19+
app.Get("/list", func(ctx iris.Context) {
20+
ctx.View("files/list")
21+
})
22+
23+
app.Get("/menu", func(ctx iris.Context) {
24+
ctx.View("menu/menu")
25+
})
26+
27+
app.Get("/list2", func(ctx iris.Context) {
28+
ctx.ViewLayout("secondary")
29+
ctx.View("files/list")
30+
})
31+
32+
app.Get("/menu2", func(ctx iris.Context) {
33+
ctx.ViewLayout("secondary")
34+
ctx.View("menu/menu")
35+
})
36+
37+
return app
38+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{{ define "title"}}
2+
<title>222</title>
3+
{{ end }}
4+
5+
{{ define "content" }}
6+
<h1>List Content</h1>
7+
{{ end }}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
{{ block "title" .}}<title>Main Default Title</title>{{end}}
4+
</head>
5+
<body>
6+
{{ block "content" .}}<h1>Main Default Content</h1>{{end}}
7+
</body>
8+
</html>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html>
2+
<head>
3+
{{ block "title" .}}<title>Secondary Default Title</title>{{end}}
4+
</head>
5+
<body>
6+
<h1>Secondary Layout</h1>
7+
{{ block "content" .}}<h1>Secondary Default Content</h1>{{end}}
8+
</body>
9+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{{ define "title" }} <title>111</title>{{ end }}
2+
3+
{{ define "content" }}<h1>Menu Content</h1>{{ end }}

aliases.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -336,18 +336,18 @@ func ConfigureMiddleware(handlers ...Handler) router.PartyConfigurator {
336336
return &partyConfiguratorMiddleware{handlers: handlers}
337337
}
338338

339-
var (
340-
// Compression is a middleware which enables
341-
// writing and reading using the best offered compression.
342-
// Usage:
343-
// app.Use (for matched routes)
344-
// app.UseRouter (for both matched and 404s or other HTTP errors).
345-
Compression = func(ctx Context) {
346-
ctx.CompressWriter(true)
347-
ctx.CompressReader(true)
348-
ctx.Next()
349-
}
339+
// Compression is a middleware which enables
340+
// writing and reading using the best offered compression.
341+
// Usage:
342+
// app.Use (for matched routes)
343+
// app.UseRouter (for both matched and 404s or other HTTP errors).
344+
func Compression(ctx Context) {
345+
ctx.CompressWriter(true)
346+
ctx.CompressReader(true)
347+
ctx.Next()
348+
}
350349

350+
var (
351351
// AllowQuerySemicolons returns a middleware that serves requests by converting any
352352
// unescaped semicolons(;) in the URL query to ampersands(&).
353353
//

core/router/api_builder.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,13 @@ func (api *APIBuilder) createRoutes(errorCode int, methods []string, relativePat
754754

755755
mainHandlerFileName, mainHandlerFileNumber := context.HandlerFileLineRel(handlers[mainHandlerIndex])
756756

757+
// TODO: think of it.
758+
if mainHandlerFileName == "<autogenerated>" {
759+
// At PartyConfigure, 2nd+ level of routes it will get <autogenerated> but in reallity will be the same as the caller.
760+
mainHandlerFileName = filename
761+
mainHandlerFileNumber = line
762+
}
763+
757764
// re-calculate mainHandlerIndex in favor of the middlewares.
758765
mainHandlerIndex = len(beginHandlers) + mainHandlerIndex
759766

0 commit comments

Comments
 (0)