package
12.2.11
Repository: https://github.com/kataras/iris.git
Documentation: pkg.go.dev

# README

View

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.

#NameParser
1HTMLhtml/template
2Blockskataras/blocks
3Djangoflosch/pongo2
4PugJoker/jade
5Handlebarsmailgun/raymond
6JetCloudyKit/jet
7Aceyosssi/ace

List of Examples.

Benchmarks.

You can serve quicktemplate files too, simply by using the Context.ResponseWriter, take a look at the iris/_examples/view/quicktemplate example.

Overview

// file: main.go
package main

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

func main() {
    app := iris.New()
    // Load all templates from the "./views" folder
    // where extension is ".html" and parse them
    // using the standard `html/template` package.
    app.RegisterView(iris.HTML("./views", ".html"))

    // 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/hello.html
        if err := ctx.View("hello.html"); err != nil {
		    ctx.HTML("<h3>%s</h3>", err.Error())
		    return
	    }
    })

    // Method:    GET
    // Resource:  http://localhost:8080/user/42
    app.Get("/user/{id:int64}", func(ctx iris.Context) {
        userID, _ := ctx.Params().GetInt64("id")
        ctx.Writef("User ID: %d", userID)
    })

    // Start the server using a network address.
    app.Listen(":8080")
}
<!-- file: ./views/hello.html -->
<html>
<head>
    <title>Hello Page</title>
</head>
<body>
    <h1>{{.message}}</h1>
</body>
</html>

Template functions

package main

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

func main() {
    app := iris.New()
    tmpl := iris.HTML("./templates", ".html")

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

    app.Get("/", hi)

    // http://localhost:8080
    app.Listen(":8080")
}

func hi(ctx iris.Context) {
    // render the template file "./templates/hi.html"
    if err := ctx.View("hi.html"); err != nil {
		ctx.HTML("<h3>%s</h3>", err.Error())
		return
	}
}
<!-- file: ./templates/hi.html -->
<b>{{greet "kataras"}}</b> <!-- will be rendered as: <b>Greetings kataras!</b> -->

Embedded

View engine supports bundled(https://github.com/go-bindata/go-bindata) template files too. Latest go-bindata release gives you a compatible http.FileSystem that can be provided as the first argument of a view engine's initialization, e.g. HTML(AssetFile(), ".html").

$ go install github.com/go-bindata/go-bindata/v3/go-bindata@latest
$ go-bindata -fs -prefix "templates" ./templates/...
$ go run .

Example Code:

package main

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

func main() {
    app := iris.New()
    app.RegisterView(iris.HTML(AssetFile(), ".html"))
    app.Get("/", hi)

    // http://localhost:8080
    app.Listen(":8080")
}

type page struct {
    Title, Name string
}

func hi(ctx iris.Context) {
    //                      {{.Page.Title}} and {{Page.Name}}
    ctx.ViewData("Page", page{Title: "Hi Page", Name: "iris"})
    if err := ctx.View("hi.html"); err != nil {
		ctx.HTML("<h3>%s</h3>", err.Error())
		return
	}
}

Examples can be found here: https://github.com/kataras/iris/tree/main/_examples/view/embedding-templates-into-app and https://github.com/kataras/iris/tree/main/_examples/view/embedding-templates-into-app-bindata.

Reload

Enable auto-reloading of templates on each request. Useful while developers are in dev mode as they no neeed to restart their app on every template edit.

Example code:

pugEngine := iris.Pug("./templates", ".jade")
pugEngine.Reload(true) // <--- set to true to re-build the templates on each request.
app.RegisterView(pugEngine)

# Functions

Ace returns a new Ace view engine.
AddJetRuntimeVars sets or inserts runtime jet variables through the Iris Context.
Blocks returns a new blocks view engine.
Django creates and returns a new django view engine.
Handlebars creates and returns a new handlebars view engine.
HTML creates and returns a new html view engine.
Jet creates and returns a new jet view engine.
Pug (or Jade) returns a new pug view engine.
WrapBlocks wraps an initialized blocks engine and returns its Iris adapter.

# Constants

JetRuntimeVarsContextKey is the Iris Context key to keep any custom jet runtime variables.
NoLayout disables the configuration's layout for a specific execution.

# Variables

AsSafeValue works like AsValue, but does not apply the 'escape' filter.
AsValue converts any given value to a pongo2.Value Usually being used within own functions passed to a template through a Context or within filter functions.

# Structs

AceEngine represents the Ace view engine.
BlocksEngine is an Iris view engine adapter for the blocks view engine.
DjangoEngine contains the django view engine structure.
HandlebarsEngine contains the handlebars view engine structure.
HTMLEngine contains the html view engine structure.
JetEngine is the jet template parser's view engine.
View is just a wrapper on top of the registered template engine.

# Type aliases

ErrNotExist reports whether a template was not found in the parsed templates tree.
JetArguments is a type alias of `jet.Arguments`, can be used on `AddFunc$funcBody`.