2017-11-07 18:12:30 +01:00
# View
2020-08-05 05:46:45 +02:00
Iris supports 8 template engines out-of-the-box, developers can still use any external golang template engine,
2020-08-03 04:46:04 +02:00
as `Context.ResponseWriter()` is an `io.Writer` .
2017-11-07 18:12:30 +01:00
2020-08-05 05:46:45 +02:00
All template engines share a common API i.e.
Parse using embedded assets, Layouts and Party-specific layout, Template Funcs, Partial Render and more.
2017-11-07 18:12:30 +01:00
2020-08-05 05:46:45 +02:00
| # | Name | Parser |
|:---|:-----------|----------|
| 1 | HTML | [html/template ](https://pkg.go.dev/html/template ) |
| 2 | Blocks | [kataras/blocks ](https://github.com/kataras/blocks ) |
| 3 | Django | [flosch/pongo2 ](https://github.com/flosch/pongo2 ) |
| 4 | Pug | [Joker/jade ](https://github.com/Joker/jade ) |
| 5 | Handlebars | [aymerick/raymond ](https://github.com/aymerick/raymond ) |
| 6 | Amber | [eknkc/amber ](https://github.com/eknkc/amber ) |
| 7 | Jet | [CloudyKit/jet ](https://github.com/CloudyKit/jet ) |
| 8 | Ace | [yosssi/ace ](https://github.com/yosssi/ace ) |
2019-06-22 20:34:19 +02:00
2020-08-06 02:35:58 +02:00
[List of Examples ](https://github.com/kataras/iris/tree/master/_examples/view ).
2019-06-22 20:34:19 +02:00
2020-08-05 05:46:45 +02:00
You can serve [quicktemplate ](https://github.com/valyala/quicktemplate ) files too, simply by using the `Context.ResponseWriter` , take a look at the [iris/_examples/view/quicktemplate ](https://github.com/kataras/iris/tree/master/_examples/view/quicktemplate ) example.
2017-11-07 18:12:30 +01:00
## Overview
```go
// file: main.go
package main
2019-10-25 00:27:02 +02:00
import "github.com/kataras/iris/v12"
2017-11-07 18:12:30 +01:00
func main() {
app := iris.New()
// Load all templates from the "./views" folder
// where extension is ".html" and parse them
// using the standard `html/template` package.
app.RegisterView(iris.HTML("./views", ".html"))
// Method: GET
// Resource: http://localhost:8080
app.Get("/", func(ctx iris.Context) {
// Bind: {{.message}} with "Hello world!"
ctx.ViewData("message", "Hello world!")
// Render template file: ./views/hello.html
ctx.View("hello.html")
})
// Method: GET
// Resource: http://localhost:8080/user/42
2020-06-07 14:26:06 +02:00
app.Get("/user/{id:int64}", func(ctx iris.Context) {
2017-11-07 18:12:30 +01:00
userID, _ := ctx.Params().GetInt64("id")
ctx.Writef("User ID: %d", userID)
})
// Start the server using a network address.
2020-03-05 21:41:27 +01:00
app.Listen(":8080")
2017-11-07 18:12:30 +01:00
}
```
```html
<!-- file: ./views/hello.html -->
< html >
< head >
< title > Hello Page< / title >
< / head >
< body >
< h1 > {{.message}}< / h1 >
< / body >
< / html >
```
## Template functions
```go
package main
2019-10-25 00:27:02 +02:00
import "github.com/kataras/iris/v12"
2017-11-07 18:12:30 +01:00
func main() {
app := iris.New()
tmpl := iris.HTML("./templates", ".html")
2019-02-23 06:23:10 +01:00
// builtin template funcs are:
2017-11-07 18:12:30 +01:00
//
// - {{ urlpath "mynamedroute" "pathParameter_ifneeded" }}
// - {{ render "header.html" }}
// - {{ render_r "header.html" }} // partial relative path to current page
// - {{ yield }}
// - {{ current }}
// register a custom template func.
tmpl.AddFunc("greet", func(s string) string {
return "Greetings " + s + "!"
})
// register the view engine to the views, this will load the templates.
app.RegisterView(tmpl)
app.Get("/", hi)
// http://localhost:8080
2020-03-05 21:41:27 +01:00
app.Listen(":8080")
2017-11-07 18:12:30 +01:00
}
func hi(ctx iris.Context) {
// render the template file "./templates/hi.html"
ctx.View("hi.html")
}
```
```html
<!-- file: ./templates/hi.html -->
< b > {{greet "kataras"}}< / b > <!-- will be rendered as: <b>Greetings kataras!</b> -->
```
## Embedded
2020-09-05 07:34:09 +02:00
View engine supports bundled(https://github.com/go-bindata/go-bindata) template files too. Latest
`go-bindata` release gives you a compatible `http.FileSystem` that can be provided as the first argument of a view engine's initialization, e.g. `HTML(AssetFile(), ".html")` .
2017-11-07 18:12:30 +01:00
2020-09-05 07:34:09 +02:00
```sh
2020-09-05 07:56:01 +02:00
$ go get -u github.com/go-bindata/go-bindata/...
2020-09-05 07:34:09 +02:00
# OR: go get -u github.com/go-bindata/go-bindata/v3/go-bindata
# to save it to your go.mod file
$ go-bindata -fs -prefix "templates" ./templates/...
$ go run .
```
Example Code:
2017-11-07 18:12:30 +01:00
```go
package main
2019-10-25 00:27:02 +02:00
import "github.com/kataras/iris/v12"
2017-11-07 18:12:30 +01:00
func main() {
app := iris.New()
2020-09-05 07:34:09 +02:00
app.RegisterView(iris.HTML(AssetFile(), ".html"))
2017-11-07 18:12:30 +01:00
app.Get("/", hi)
// http://localhost:8080
2020-03-05 21:41:27 +01:00
app.Listen(":8080")
2017-11-07 18:12:30 +01:00
}
type page struct {
Title, Name string
}
func hi(ctx iris.Context) {
// {{.Page.Title}} and {{Page.Name}}
ctx.ViewData("Page", page{Title: "Hi Page", Name: "iris"})
ctx.View("hi.html")
}
```
A real example can be found here: https://github.com/kataras/iris/tree/master/_examples/view/embedding-templates-into-app.
## Reload
Enable auto-reloading of templates on each request. Useful while developers are in dev mode
as they no neeed to restart their app on every template edit.
Example code:
```go
pugEngine := iris.Pug("./templates", ".jade")
pugEngine.Reload(true) // < --- set to true to re-build the templates on each request .
app.RegisterView(pugEngine)
2019-06-22 20:34:19 +02:00
```