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:
35
_examples/view/template_blocks_0/main.go
Normal file
35
_examples/view/template_blocks_0/main.go
Normal 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)
|
||||
}
|
||||
12
_examples/view/template_blocks_0/views/500.html
Normal file
12
_examples/view/template_blocks_0/views/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 }}
|
||||
1
_examples/view/template_blocks_0/views/index.html
Normal file
1
_examples/view/template_blocks_0/views/index.html
Normal file
@@ -0,0 +1 @@
|
||||
<h1>Index Body</h1>
|
||||
13
_examples/view/template_blocks_0/views/layouts/error.html
Normal file
13
_examples/view/template_blocks_0/views/layouts/error.html
Normal 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>
|
||||
13
_examples/view/template_blocks_0/views/layouts/main.html
Normal file
13
_examples/view/template_blocks_0/views/layouts/main.html
Normal 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>
|
||||
@@ -0,0 +1 @@
|
||||
<h3>Footer Partial</h3>
|
||||
Reference in New Issue
Block a user