iris/view
Gerasimos (Makis) Maropoulos e05333475a minor
Former-commit-id: eb7899a04d02dd002670a2b52bd1e2a6fd7ce7db
2019-08-09 08:29:22 +03:00
..
amber.go resolve conflicts with current master v11.1.1 2019-07-23 19:20:07 +03:00
django.go resolve conflicts with current master v11.1.1 2019-07-23 19:20:07 +03:00
engine.go Implement a new View Engine for the Jet template parser as requested at: https://github.com/kataras/iris/issues/1281 2019-06-22 21:34:19 +03:00
funcs.go Add notes for the new lead maintainer of the open-source iris project and align with @get-ion/ion by @hiveminded 2017-07-10 18:32:42 +03:00
handlebars.go resolve conflicts with current master v11.1.1 2019-07-23 19:20:07 +03:00
html.go resolve conflicts with current master v11.1.1 2019-07-23 19:20:07 +03:00
jet.go minor 2019-08-09 08:29:22 +03:00
pug.go Update vendor for Pug (Jade) Parser and add Iris + Pug examples in the _examples/view folder, relative to https://github.com/kataras/iris/issues/1003 2018-05-26 22:49:48 +03:00
README.md resolve conflicts with current master v11.1.1 2019-07-23 19:20:07 +03:00
view.go fix https://github.com/kataras/iris/issues/1327 2019-08-05 20:37:44 +03:00

View

Iris supports 6 template engines out-of-the-box, developers can still use any external golang template engine, as context/context#ResponseWriter() is an io.Writer.

All of these six template engines have common features with common API, like Layout, Template Funcs, Party-specific layout, partial rendering and more.

Examples

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

Overview

// file: main.go
package main

import "github.com/kataras/iris"

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

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

    // Start the server using a network address.
    app.Run(iris.Addr(":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"

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.Run(iris.Addr(":8080"))
}

func hi(ctx iris.Context) {
    // render the template file "./templates/hi.html"
    ctx.View("hi.html")
}
<!-- file: ./templates/hi.html -->
<b>{{greet "kataras"}}</b> <!-- will be rendered as: <b>Greetings kataras!</b> -->

Embedded

View engine supports bundled(https://github.com/shuLhan/go-bindata) template files too. go-bindata gives you two functions, Assset and AssetNames, these can be set to each of the template engines using the .Binary function.

Example code:

package main

import "github.com/kataras/iris"

func main() {
    app := iris.New()
    // $ go get -u github.com/shuLhan/go-bindata/...
    // $ go-bindata ./templates/...
    // $ go build
    // $ ./embedding-templates-into-app
    // html files are not used, you can delete the folder and run the example
    app.RegisterView(iris.HTML("./templates", ".html").Binary(Asset, AssetNames))
    app.Get("/", hi)

    // http://localhost:8080
    app.Run(iris.Addr(":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"})
    ctx.View("hi.html")
}

A real example can be found here: https://github.com/kataras/iris/tree/master/_examples/view/embedding-templates-into-app.

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)