Template rendering

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.

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

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

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!".

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

Root binding:

ctx.View("user-page.html", User{})

// root binding as {{.Name}}

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

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

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

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.

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

Example Code:

// file: main.go
package main

import "github.com/kataras/iris/v12"

func main() {
    app := iris.New()

    // Parse all templates from the "./views" folder
    // where extension is ".html" and parse them
    // using the standard `html/template` package.
    tmpl := iris.HTML("./views", ".html")
    // Set custom delimeters.
    tmpl.Delims("{{", "}}")
    // Enable re-build on local template files changes.
    tmpl.Reload(true)

    // Default template funcs are:
    //
    // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
    // - {{ render "header.html" }}
    // and partial relative path to current page:
    // - {{ render_r "header.html" }} 
    // - {{ 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)

    // 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/hi.html
        ctx.View("hi.html")
    })

    app.Listen(":8080")
}
<!-- file: ./views/hi.html -->
<html>
<head>
    <title>Hi Page</title>
</head>
<body>
    <h1>{{.message}}</h1>
    <strong>{{greet "to you"}}</strong>
</body>
</html>

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

The rendered result will look like this:

<html>
<head>
    <title>Hi Page</title>
</head>
<body>
    <h1>Hello world!</h1>
    <strong>Greetings to you!</strong>
</body>
</html>