From 600eb645dfb7b5cf98d8e1c271e39309e870657f Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Sun, 30 Aug 2020 15:26:50 +0300 Subject: [PATCH] minor: examples --- _examples/README.md | 3 +- _examples/view/overview/main.go | 52 +++++++++++++------ .../view/overview/templates/example.html | 27 ++++++++++ _examples/view/template_handlebars_0/main.go | 34 ++++++++++++ .../templates/example.html | 27 ++++++++++ _examples/view/template_html_0/main.go | 13 ----- 6 files changed, 125 insertions(+), 31 deletions(-) create mode 100644 _examples/view/overview/templates/example.html create mode 100644 _examples/view/template_handlebars_0/main.go create mode 100644 _examples/view/template_handlebars_0/templates/example.html diff --git a/_examples/README.md b/_examples/README.md index ed88f16c..8e152b12 100644 --- a/_examples/README.md +++ b/_examples/README.md @@ -120,7 +120,8 @@ * [Jet Embedded](view/template_jet_1_embedded) * [Jet 'urlpath' tmpl func](view/template_jet_2) * [Jet Template Funcs from Struct](view/template_jet_3) - - [Ace](view/template_ace_0) + * [Ace](view/template_ace_0) + * [Handlebars](view/template_handlebars_0) * Third-Parties * [Render `valyala/quicktemplate` templates](view/quicktemplate) * [Render `shiyanhui/hero` templates](view/herotemplate) diff --git a/_examples/view/overview/main.go b/_examples/view/overview/main.go index 8aeaabf1..caef76a6 100644 --- a/_examples/view/overview/main.go +++ b/_examples/view/overview/main.go @@ -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(...) -*/ diff --git a/_examples/view/overview/templates/example.html b/_examples/view/overview/templates/example.html new file mode 100644 index 00000000..53725068 --- /dev/null +++ b/_examples/view/overview/templates/example.html @@ -0,0 +1,27 @@ + + + + + + HTML Template Example + + +

{{.Name}}

+

{{.Age}}

+ +

Slice

+ {{range $idx, $item := .Items}} + {{ $idx }} - {{ $item }}
+ {{end}} +

Map

+ {{range $key, $value := .Map}} + {{ $key }} - {{ $value }}
+ {{end}} + +
+ + Read more at: + https://golang.org/pkg/html/template + + + \ No newline at end of file diff --git a/_examples/view/template_handlebars_0/main.go b/_examples/view/template_handlebars_0/main.go new file mode 100644 index 00000000..c308a59e --- /dev/null +++ b/_examples/view/template_handlebars_0/main.go @@ -0,0 +1,34 @@ +package main + +import "github.com/kataras/iris/v12" + +func main() { + app := iris.New() + + // Init the handlebars engine + engine := iris.Handlebars("./templates", ".html").Reload(true) + // Register a helper. + engine.AddFunc("fullName", func(person map[string]string) string { + return person["firstName"] + " " + person["lastName"] + }) + + app.RegisterView(engine) + + app.Get("/", func(ctx iris.Context) { + viewData := iris.Map{ + "author": map[string]string{"firstName": "Jean", "lastName": "Valjean"}, + "body": "Life is difficult", + "comments": []iris.Map{{ + "author": map[string]string{"firstName": "Marcel", "lastName": "Beliveau"}, + "body": "LOL!", + }}, + } + + ctx.View("example.html", viewData) + }) + + // Read more about its syntax at: + // https://github.com/aymerick/raymond and + // https://handlebarsjs.com/guide + app.Listen(":8080") +} diff --git a/_examples/view/template_handlebars_0/templates/example.html b/_examples/view/template_handlebars_0/templates/example.html new file mode 100644 index 00000000..ffbcc8e6 --- /dev/null +++ b/_examples/view/template_handlebars_0/templates/example.html @@ -0,0 +1,27 @@ +
+

By {{fullName author}}

+
{{body}}
+ +

Comments

+ + {{#each comments}} +

By {{fullName author}}

+
{{body}}
+ {{/each}} + + + + +
+ + Read more at: + https://github.com/aymerick/raymond and + https://handlebarsjs.com/guide + + +
\ No newline at end of file diff --git a/_examples/view/template_html_0/main.go b/_examples/view/template_html_0/main.go index ead5ec56..71ef18fc 100644 --- a/_examples/view/template_html_0/main.go +++ b/_examples/view/template_html_0/main.go @@ -33,16 +33,3 @@ func hi(ctx iris.Context) { // ctx.ViewData("", myCcustomStruct{}) ctx.View("hi.html") } - -/* -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(...) -*/