Template rendering

Iris supports 8 template engines out-of-the-box, developers can still use any external golang template engine, as Context.ResponseWriter() is an io.Writer.

All template engines share a common API i.e. Parse using embedded assets, Layouts and Party-specific layout, Template Funcs, Partial Render and more.

# Name Parser
1 HTML html/template
2 Blocks kataras/blocks
3 Django flosch/pongo2
4 Pug Joker/jade
5 Handlebars aymerick/raymond
6 Amber eknkc/amber
7 Jet CloudyKit/jet
8 Ace yosssi/ace

List of Examples.

List of Benchmarks.

A view engine can be registered per-Party. To register a view engine use the Application/Party.RegisterView(ViewEngine) method as shown below.

Load all templates from the “./views” folder where extension is “.html” and parse them using the standard html/template package.

  1. // [app := iris.New...]
  2. tmpl := iris.HTML("./views", ".html")
  3. app.RegisterView(tmpl)

To render or execute a view use the Context.View method inside the main route’s handler.

  1. ctx.View("hi.html")

To bind Go values with key-value pattern inside a view through middleware or main handler use the Context.ViewData method before the Context.View one.

Bind: {{.message}} with "Hello world!".

  1. ctx.ViewData("message", "Hello world!")

Root binding:

  1. ctx.View("user-page.html", User{})
  2. // root binding as {{.Name}}

To add a template function use the AddFunc method of the preferred view engine.

  1. // func name, input arguments, render value
  2. tmpl.AddFunc("greet", func(s string) string {
  3. return "Greetings " + s + "!"
  4. })

To reload on every request call the view engine’s Reload method.

  1. tmpl.Reload(true)

To use embedded templates and not depend on local file system use the go-bindata external tool and pass its AssetFile() generated function to the first input argument of the preferred view engine.

  1. tmpl := iris.HTML(AssetFile(), ".html")

Example Code:

  1. // file: main.go
  2. package main
  3. import "github.com/kataras/iris/v12"
  4. func main() {
  5. app := iris.New()
  6. // Parse all templates from the "./views" folder
  7. // where extension is ".html" and parse them
  8. // using the standard `html/template` package.
  9. tmpl := iris.HTML("./views", ".html")
  10. // Set custom delimeters.
  11. tmpl.Delims("{{", "}}")
  12. // Enable re-build on local template files changes.
  13. tmpl.Reload(true)
  14. // Default template funcs are:
  15. //
  16. // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
  17. // - {{ render "header.html" }}
  18. // and partial relative path to current page:
  19. // - {{ render_r "header.html" }}
  20. // - {{ yield }}
  21. // - {{ current }}
  22. // Register a custom template func:
  23. tmpl.AddFunc("greet", func(s string) string {
  24. return "Greetings " + s + "!"
  25. })
  26. // Register the view engine to the views,
  27. // this will load the templates.
  28. app.RegisterView(tmpl)
  29. // Method: GET
  30. // Resource: http://localhost:8080
  31. app.Get("/", func(ctx iris.Context) {
  32. // Bind: {{.message}} with "Hello world!"
  33. ctx.ViewData("message", "Hello world!")
  34. // Render template file: ./views/hi.html
  35. ctx.View("hi.html")
  36. })
  37. app.Listen(":8080")
  38. }
  1. <!-- file: ./views/hi.html -->
  2. <html>
  3. <head>
  4. <title>Hi Page</title>
  5. </head>
  6. <body>
  7. <h1>{{.message}}</h1>
  8. <strong>{{greet "to you"}}</strong>
  9. </body>
  10. </html>

Open a browser tab at http://localhost:8080.

The rendered result will look like this:

  1. <html>
  2. <head>
  3. <title>Hi Page</title>
  4. </head>
  5. <body>
  6. <h1>Hello world!</h1>
  7. <strong>Greetings to you!</strong>
  8. </body>
  9. </html>