Documentation

Iris supports 7 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.

List of Examples.

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)

Render

To render or execute a view use the Context.View method inside the main route's handler. E.g. to load the ./views/hi.html template file:

ctx.View("hi") // or hi.html

Pass Data

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

To bind a Go model to a view you have two options:

1.

2.

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

To reload on each request set the view engine's Reload method.

Embedded

To use embedded templates and not depend on local file system use the go-bindata external tool and pass its Asset and AssetNames functions to the Binary method of the preferred view engine.

Example Code:

Please read the comments too.

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

The rendered result will look like this:

Multitemplate

Iris allows unlimited number of registered view engines per Application. Besides that, you can register a view engine per Party or through middleware too!.

Through Middleware

Usage

Last updated

Was this helpful?