mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:16:28 +01:00
minor: examples
This commit is contained in:
parent
a3279b8fa9
commit
600eb645df
|
@ -120,7 +120,8 @@
|
||||||
* [Jet Embedded](view/template_jet_1_embedded)
|
* [Jet Embedded](view/template_jet_1_embedded)
|
||||||
* [Jet 'urlpath' tmpl func](view/template_jet_2)
|
* [Jet 'urlpath' tmpl func](view/template_jet_2)
|
||||||
* [Jet Template Funcs from Struct](view/template_jet_3)
|
* [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
|
* Third-Parties
|
||||||
* [Render `valyala/quicktemplate` templates](view/quicktemplate)
|
* [Render `valyala/quicktemplate` templates](view/quicktemplate)
|
||||||
* [Render `shiyanhui/hero` templates](view/herotemplate)
|
* [Render `shiyanhui/hero` templates](view/herotemplate)
|
||||||
|
|
|
@ -12,26 +12,44 @@ func main() {
|
||||||
// - {{ render_r "header.html" }} // partial relative path to current page
|
// - {{ render_r "header.html" }} // partial relative path to current page
|
||||||
// - {{ yield }}
|
// - {{ yield }}
|
||||||
// - {{ current }}
|
// - {{ 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) {
|
app.Get("/", func(ctx iris.Context) {
|
||||||
ctx.CompressWriter(true) // enable compression based on Accept-Encoding (e.g. "gzip").
|
// enable compression based on Accept-Encoding (e.g. "gzip"),
|
||||||
ctx.ViewData("Name", "iris") // the .Name inside the ./templates/hi.html.
|
// alternatively: app.Use(iris.Compression).
|
||||||
ctx.View("hi.html") // render the template with the file name relative to the './templates'.
|
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/
|
// http://localhost:8080/
|
||||||
app.Listen(":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(...)
|
|
||||||
*/
|
|
||||||
|
|
27
_examples/view/overview/templates/example.html
Normal file
27
_examples/view/overview/templates/example.html
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>HTML Template Example</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2>{{.Name}}</h2>
|
||||||
|
<h3>{{.Age}}</h3>
|
||||||
|
|
||||||
|
<h4>Slice</h4>
|
||||||
|
{{range $idx, $item := .Items}}
|
||||||
|
{{ $idx }} - {{ $item }} <br/>
|
||||||
|
{{end}}
|
||||||
|
<h4>Map</h4>
|
||||||
|
{{range $key, $value := .Map}}
|
||||||
|
{{ $key }} - {{ $value }} <br/>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
|
<hr/>
|
||||||
|
|
||||||
|
<strong>Read more at: <a href="https://golang.org/pkg/html/template" target="_new">
|
||||||
|
https://golang.org/pkg/html/template</a>
|
||||||
|
</strong>
|
||||||
|
</body>
|
||||||
|
</html>
|
34
_examples/view/template_handlebars_0/main.go
Normal file
34
_examples/view/template_handlebars_0/main.go
Normal file
|
@ -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")
|
||||||
|
}
|
27
_examples/view/template_handlebars_0/templates/example.html
Normal file
27
_examples/view/template_handlebars_0/templates/example.html
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<div class="post">
|
||||||
|
<h1>By {{fullName author}}</h1>
|
||||||
|
<div class="body">{{body}}</div>
|
||||||
|
|
||||||
|
<h1>Comments</h1>
|
||||||
|
|
||||||
|
{{#each comments}}
|
||||||
|
<h2>By {{fullName author}}</h2>
|
||||||
|
<div class="body">{{body}}</div>
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
|
|
||||||
|
<!-- <h1>Struct example</h1>
|
||||||
|
{{#with myStruct}}
|
||||||
|
{{ this }}
|
||||||
|
OR
|
||||||
|
{{ FieldMame }}
|
||||||
|
{{/with}} -->
|
||||||
|
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<strong>Read more at: <a href="https://github.com/aymerick/raymond" target="_new">
|
||||||
|
https://github.com/aymerick/raymond</a> and <a href="https://handlebarsjs.com/guide" target="_new">
|
||||||
|
https://handlebarsjs.com/guide
|
||||||
|
</a>
|
||||||
|
</strong>
|
||||||
|
</div>
|
|
@ -33,16 +33,3 @@ func hi(ctx iris.Context) {
|
||||||
// ctx.ViewData("", myCcustomStruct{})
|
// ctx.ViewData("", myCcustomStruct{})
|
||||||
ctx.View("hi.html")
|
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(...)
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user