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

minor: examples

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-30 15:26:50 +03:00
parent a3279b8fa9
commit 600eb645df
6 changed files with 125 additions and 31 deletions

View File

@@ -12,26 +12,44 @@ func main() {
// - {{ render_r "header.html" }} // partial relative path to current page
// - {{ yield }}
// - {{ current }}
app.RegisterView(iris.HTML("./templates", ".html"))
app.RegisterView(iris.HTML("./templates", ".html").
Reload(true)) // Set Reload false to production.
app.Get("/", func(ctx iris.Context) {
ctx.CompressWriter(true) // enable compression based on Accept-Encoding (e.g. "gzip").
ctx.ViewData("Name", "iris") // the .Name inside the ./templates/hi.html.
ctx.View("hi.html") // render the template with the file name relative to the './templates'.
// 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'.
ctx.View("hi.html")
})
app.Get("/example_map", func(ctx iris.Context) {
ctx.View("example.html", 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"},
})
})
app.Get("/example_struct", func(ctx iris.Context) {
var examplePage = struct {
Name string
Age int
Items []string
Map map[string]interface{}
}{
"Example Name",
42,
[]string{"Example slice entry 1", "entry 2", "entry 3"},
iris.Map{"map key": "map value", "other key": "other value"},
}
ctx.View("example.html", examplePage)
})
// http://localhost:8080/
app.Listen(":8080")
}
/*
Note:
In case you're wondering, the code behind the view engines derives from the "github.com/kataras/iris/v12/view" package,
access to the engines' variables can be granded by "github.com/kataras/iris/v12" package too.
iris.HTML(...) is a shortcut of view.HTML(...)
iris.Django(...) >> >> view.Django(...)
iris.Pug(...) >> >> view.Pug(...)
iris.Handlebars(...) >> >> view.Handlebars(...)
iris.Amber(...) >> >> view.Amber(...)
*/