Template Rendering

Iris has built-in support for eight template engines, and developers can still use any external Go language template engine because Context.ResponseWriter() is an io.Writer.

All template engines share a common API, including parsing using embedded resources, layout and Party-specific layout, template functions, and partial rendering.

# 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

You can register a view engine for each Party. Use the Application/Party.RegisterView(ViewEngine) method to register, as shown below:

Load all templates with the extension ".html" from the "./views" folder and parse using the standard html/template package.

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

In the main route handler, use the Context.View method to render or execute a view.

if err := ctx.View("hi.html"); err != nil {
    ctx.HTML("%s", err.Error())
    return
}

Use the Context.ViewData method in middleware or main handler to bind Go values with key-value patterns in the view.

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

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

Root binding:

if err := ctx.View("user-page.html", User{}); err != nil {
    ctx.HTML("%s", err.Error())
    return
}

// root binding as {{.Name}}

Use the AddFunc method of the preferred view engine to add template functions.

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

If you want to reload on every request, call the view engine's Reload method.

tmpl.Reload(true)

If you want to use embedded templates without relying on the local file system, use the external tool go-bindata and pass the AssetFile() function it generates as the first input parameter to the preferred view engine.

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

Sample code:

// File: main.go
package main

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

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

    // Parse all templates with the ".html" suffix from the "./views" folder
    // Use the standard `html/template` package for parsing
    tmpl := iris.HTML("./views", ".html")
    // Set custom delimiters
    tmpl.Delims("{{", "}}")
    // Enable re-compilation for local template file changes
    tmpl.Reload(true)
    
    // Default template functions include:
    //
    // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
    // - {{ render "header.html" . }}
    // And the relative path to the current page:
    // - {{ render_r "header.html" . }} 
    // - {{ yield . }}
    // - {{ current }}
    // Register a custom template function:
    tmpl.AddFunc("greet", func(s string) string {
        return "Greetings " + s + "!"
    })
    
    // Register the view engine to the application,
    // this will load the templates.
    app.RegisterView(tmpl)
    
    // Method: GET
    // Resource: http://localhost:8080
    app.Get("/", func(ctx iris.Context) {
        // Bind {{.message}} to "Hello world!"
        ctx.ViewData("message", "Hello world!")
        // Render the template file: ./views/hi.html
        if err := ctx.View("hi.html"); err != nil {
            ctx.HTML("%s", err.Error())
            return
        }
    })
    
    app.Listen(":8080")
}
Hi Page


{{.message}}
{{greet "to you"}}

Open a new tab in your browser and visit http://localhost:8080.

The rendered result should be as follows:

    Hi Page


    Hello world!
    Greetings to you!