1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-18 02:17:05 +00:00

examples: add an example for #2019

This commit is contained in:
Gerasimos (Makis) Maropoulos
2022-12-17 01:16:10 +02:00
parent 1ea5cd58be
commit de8883fc98
6 changed files with 58 additions and 7 deletions

View File

@@ -1,6 +1,17 @@
package main
import "github.com/kataras/iris/v12"
import (
"html/template"
"time"
"github.com/kataras/iris/v12"
)
// ViewFunctions presents some builtin functions
// for html view engines. See `View.Funcs` or `view/html.Funcs` and etc.
var Functions = template.FuncMap{
"Now": time.Now,
}
func main() {
app := iris.New()
@@ -13,7 +24,8 @@ func main() {
// - {{ yield . }}
// - {{ current . }}
app.RegisterView(iris.HTML("./templates", ".html").
Reload(true)) // Set Reload false to production.
Funcs(Functions). // Optionally register some more builtin functions.
Reload(false)) // Set Reload to true on development.
app.Get("/", func(ctx iris.Context) {
// enable compression based on Accept-Encoding (e.g. "gzip"),
@@ -30,13 +42,15 @@ func main() {
})
app.Get("/example_map", func(ctx iris.Context) {
if err := ctx.View("example.html", iris.Map{
examplePage := iris.Map{
"Name": "Example Name",
"Age": 42,
"Items": []string{"Example slice entry 1", "entry 2", "entry 3"},
"Map": iris.Map{"map key": "map value", "other key": "other value"},
"Nested": iris.Map{"Title": "Iris E-Book", "Pages": 620},
}); err != nil {
}
if err := ctx.View("example.html", examplePage); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
@@ -71,6 +85,23 @@ func main() {
}
})
app.Get("/functions", func(ctx iris.Context) {
var functionsPage = struct {
// A function.
Now func() time.Time
// A struct field which contains methods.
Ctx iris.Context
}{
Now: time.Now,
Ctx: ctx,
}
if err := ctx.View("functions.html", functionsPage); err != nil {
ctx.HTML("<h3>%s</h3>", err.Error())
return
}
})
// http://localhost:8080/
app.Listen(":8080")
}

View File

@@ -0,0 +1,3 @@
<h1>Function: {{ Now }}</h1>
<h1>Field: {{ .Ctx.Request.URL.Path }} </h1>
<h1>Field Struct's Function (Method): {{ .Ctx.FullRequestURI }} </h1>