Golang fiber c.Render layout not excute right template

711 Views Asked by At

I'm trying to use one layout with different templates,

├── Main Folder
   ├── cmd
        └── main.go
   ├── controllers
   ├── models
   ├── views
        └── partials
            └── layout.html
        └── index.html
        └── dashboard.html
        └── login.html
   └── public
        └── sample.png

In my layout.html, I have

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>Loaded from Layout</h1>
  {{ template . }}
</body>
</html>

In my dashboard.html

{{ define "dashboard" }}
    <h1>Dashboard</h1>
{{ end }}

In my login.html

{{ define "login" }}
    <h1>Login</h1>
{{ end }}

When I'm render dashboard through controller its only load login.html, its take last html file always

// controller

func Dashboard(c *fiber.Ctx) error {
    return c.Render("dashboard", fiber.Map{
        "title": "Login | Selectify Admin",
    }, "partials/layout")
}

In main file I have told where to find views

//main.go

// create a new HTML engine
    template_engine := html.New(
        "./views",
        ".html",
    )

    // create a new Fiber app
    app := fiber.New(fiber.Config{
        Views: template_engine, // set the views engine

        ErrorHandler: func(c *fiber.Ctx, err error) error {
            return utils.HandleError(c, err)
        },
    })

it's giving an error "template: "partials/layout" is an incomplete or empty template" How can I send or render correct html file using one layout??

Go version 1.19 fiver version 2 "github.com/gofiber/template/html"

1

There are 1 best solutions below

0
On BEST ANSWER

Found the answer, instead of template we gotta use {{embed}}, then it'll include right temlate which you pass from controller


<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>Loaded from Layout</h1>
  {{ embed  }}
</body>
</html>