1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-07 20:17:05 +00:00

add 'iris.Blocks' template engine

read more about its features at: https://github.com/kataras/blocks
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-05 06:46:45 +03:00
parent 6844be57ea
commit b363492cca
24 changed files with 631 additions and 40 deletions

View File

@@ -0,0 +1,35 @@
package main
import "github.com/kataras/iris/v12"
func main() {
app := iris.New()
// Read about its syntax at: https://github.com/kataras/blocks
app.RegisterView(iris.Blocks("./views", ".html").Reload(true))
app.Get("/", index)
app.Get("/500", internalServerError)
app.Listen(":8080")
}
func index(ctx iris.Context) {
data := iris.Map{
"Title": "Page Title",
}
ctx.ViewLayout("main")
ctx.View("index", data)
}
func internalServerError(ctx iris.Context) {
ctx.StatusCode(iris.StatusInternalServerError)
data := iris.Map{
"Code": iris.StatusInternalServerError,
"Message": "Internal Server Error",
}
ctx.ViewLayout("error")
ctx.View("500", data)
}

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>