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 Benchmarks.

    A view engine can be registered per-Party. To register a view engine use the Application/Party.RegisterView(ViewEngine) method as shown below.

    To render or execute a view use the Context.View method inside the main route’s handler.

    1. ctx.View("hi.html")

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

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

    Root binding:

    1. // func name, input arguments, render value
    2. tmpl.AddFunc("greet", func(s string) string {
    3. return "Greetings " + s + "!"
    4. })

    To reload on every request call the view engine’s Reload method.

    1. tmpl.Reload(true)

    To use embedded templates and not depend on local file system use the external tool and pass its AssetFile() generated function to the first input argument of the preferred view engine.

    Example Code:

    1. // file: main.go
    2. package main
    3. import "github.com/kataras/iris/v12"
    4. app := iris.New()
    5. // Parse all templates from the "./views" folder
    6. // where extension is ".html" and parse them
    7. // using the standard `html/template` package.
    8. tmpl := iris.HTML("./views", ".html")
    9. // Set custom delimeters.
    10. // Enable re-build on local template files changes.
    11. tmpl.Reload(true)
    12. // Default template funcs are:
    13. //
    14. // - {{ urlpath "myNamedRoute" "pathParameter_ifNeeded" }}
    15. // - {{ render "header.html" }}
    16. // and partial relative path to current page:
    17. // - {{ render_r "header.html" }}
    18. // - {{ yield }}
    19. // - {{ current }}
    20. // Register a custom template func:
    21. tmpl.AddFunc("greet", func(s string) string {
    22. return "Greetings " + s + "!"
    23. })
    24. // Register the view engine to the views,
    25. // this will load the templates.
    26. app.RegisterView(tmpl)
    27. // Resource: http://localhost:8080
    28. app.Get("/", func(ctx iris.Context) {
    29. // Bind: {{.message}} with "Hello world!"
    30. ctx.ViewData("message", "Hello world!")
    31. // Render template file: ./views/hi.html
    32. ctx.View("hi.html")
    33. })
    34. app.Listen(":8080")
    35. }
    1. <!-- file: ./views/hi.html -->
    2. <html>
    3. <head>
    4. <title>Hi Page</title>
    5. </head>
    6. <body>
    7. <h1>{{.message}}</h1>
    8. <strong>{{greet "to you"}}</strong>
    9. </html>

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