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.
To reload on each request set the view engine's Reload method.
tmpl.Reload(true)
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.
tmpl.Binary(Asset, AssetNames)
Example Code:
Please read the comments too.
// file: main.gopackagemainimport"github.com/kataras/iris/v12"funcmain() { 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")// 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")}
<html><head> <title>Hi Page</title></head><body> <h1>Hello world!</h1> <strong>Greetings to you!</strong></body></html>
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!.
// Register a view engine per group of routes.adminGroup := app.Party("/admin")adminGroup.RegisterView(iris.Blocks("./views/admin", ".html"))
// Register a view engine on-fly for the current chain of handlers.views := iris.Blocks("./views/on-fly", ".html")views.Load()app.Get("/", setViews(views), onFly)