2017-07-10 17:32:42 +02:00
|
|
|
package main
|
|
|
|
|
2022-12-17 00:16:10 +01:00
|
|
|
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,
|
|
|
|
}
|
2017-07-10 17:32:42 +02:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
|
|
|
|
// with default template funcs:
|
|
|
|
//
|
|
|
|
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
|
2022-12-13 00:37:15 +01:00
|
|
|
// - {{ render "header.html" . }}
|
|
|
|
// - {{ render_r "header.html" . }} // partial relative path to current page
|
|
|
|
// - {{ yield . }}
|
|
|
|
// - {{ current . }}
|
2020-08-30 14:26:50 +02:00
|
|
|
app.RegisterView(iris.HTML("./templates", ".html").
|
2022-12-17 00:16:10 +01:00
|
|
|
Funcs(Functions). // Optionally register some more builtin functions.
|
|
|
|
Reload(false)) // Set Reload to true on development.
|
2020-08-30 14:26:50 +02:00
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2020-08-30 14:26:50 +02:00
|
|
|
// enable compression based on Accept-Encoding (e.g. "gzip"),
|
|
|
|
// alternatively: app.Use(iris.Compression).
|
|
|
|
ctx.CompressWriter(true)
|
|
|
|
// the .Name inside the ./templates/hi.html.
|
|
|
|
ctx.ViewData("Name", "iris")
|
|
|
|
// render the template with the file name relative to the './templates'.
|
2020-09-29 18:19:19 +02:00
|
|
|
// file extension is OPTIONAL.
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("hi.html"); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2017-07-10 17:32:42 +02:00
|
|
|
})
|
|
|
|
|
2020-08-30 14:26:50 +02:00
|
|
|
app.Get("/example_map", func(ctx iris.Context) {
|
2022-12-17 00:16:10 +01:00
|
|
|
examplePage := iris.Map{
|
2020-08-30 16:18:04 +02:00
|
|
|
"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},
|
2022-12-17 00:16:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ctx.View("example.html", examplePage); err != nil {
|
2022-12-13 00:37:15 +01:00
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-08-30 14:26:50 +02:00
|
|
|
})
|
2017-07-10 17:32:42 +02:00
|
|
|
|
2020-08-30 14:26:50 +02:00
|
|
|
app.Get("/example_struct", func(ctx iris.Context) {
|
2020-08-30 16:18:04 +02:00
|
|
|
type book struct {
|
|
|
|
Title string
|
|
|
|
Pages int
|
|
|
|
}
|
|
|
|
|
2020-08-30 14:26:50 +02:00
|
|
|
var examplePage = struct {
|
2020-08-30 16:18:04 +02:00
|
|
|
Name string
|
|
|
|
Age int
|
|
|
|
Items []string
|
|
|
|
Map map[string]interface{}
|
|
|
|
Nested book
|
2020-08-30 14:26:50 +02:00
|
|
|
}{
|
|
|
|
"Example Name",
|
|
|
|
42,
|
|
|
|
[]string{"Example slice entry 1", "entry 2", "entry 3"},
|
|
|
|
iris.Map{"map key": "map value", "other key": "other value"},
|
2020-08-30 16:18:04 +02:00
|
|
|
book{
|
|
|
|
"Iris E-Book",
|
|
|
|
620,
|
|
|
|
},
|
2020-08-30 14:26:50 +02:00
|
|
|
}
|
2017-07-10 17:32:42 +02:00
|
|
|
|
2022-12-13 00:37:15 +01:00
|
|
|
if err := ctx.View("example.html", examplePage); err != nil {
|
|
|
|
ctx.HTML("<h3>%s</h3>", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2020-08-30 14:26:50 +02:00
|
|
|
})
|
2017-07-10 17:32:42 +02:00
|
|
|
|
2022-12-17 00:16:10 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-08-30 14:26:50 +02:00
|
|
|
// http://localhost:8080/
|
|
|
|
app.Listen(":8080")
|
|
|
|
}
|