Skip to content

Latest commit

 

History

History
96 lines (78 loc) · 2.37 KB

templates.md

File metadata and controls

96 lines (78 loc) · 2.37 KB
description
Fiber supports server-side template engines.

📝 Templates

Template interfaces

Fiber provides a Views interface to provide your own template engine:

{% tabs %} {% tab title="Views" %}

type Views interface {
    Load() error
    Render(io.Writer, string, interface{}, ...string) error
}

{% endtab %} {% endtabs %}

Views interface contains a Load and Render method, Load is executed by Fiber on app initialization to load/parse the templates.

// Pass engine to Fiber's Views Engine
app := fiber.New(fiber.Config{
    Views: engine,
    // Views Layout is the global layout for all template render until override on Render function.
    ViewsLayout: "layouts/main"
})

The Render method is linked to the ctx.Render() function that accepts a template name and binding data. It will use global layout if layout is not being defined in Render function

app.Get("/", func(c *fiber.Ctx) error {
    return c.Render("index", fiber.Map{
        "hello": "world",
    });
})

Engines

Fiber team maintains templates package that provides wrappers for multiple template engines:

{% tabs %} {% tab title="example" %}

package main

import (
    "log"
    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/template/html"
)

func main() {
    // Initialize standard Go html template engine
    engine := html.New("./views", ".html")

    app := fiber.New(fiber.Config{
        Views: engine,
    })
    app.Get("/", func(c *fiber.Ctx) error {
        // Render index template
        return c.Render("index", fiber.Map{
            "Title": "Hello, World!",
        })
    })

    log.Fatal(app.Listen(":3000"))
}

{% endtab %}

{% tab title="views/index.html" %}

<!DOCTYPE html>
<body>
    <h1>{{.Title}}</h1>
</body>
</html>

{% endtab %} {% endtabs %}