Home > Backend Development > Golang > Golang Fiber Template Engine HTML: Rendering: Template does not exist

Golang Fiber Template Engine HTML: Rendering: Template does not exist

WBOY
Release: 2024-02-11 12:30:09
forward
769 people have browsed it

Golang Fiber 模板引擎 HTML:渲染:模板不存在

In Golang development, the template engine is a very important part, it can help developers render HTML pages more conveniently. As Golang's lightweight web framework, Fiber also provides its own template engine. In the process of using Fiber, you sometimes encounter the "template does not exist" problem. This problem may be caused by incorrect path settings or the file does not exist. In this article, PHP editor Youzi will analyze the cause of this problem in detail and provide solutions to help you better use the Fiber template engine for HTML rendering.

Question content

On my ubuntu 22.10 digitalocean server, I'm trying to use golang and fiber with the html template engine. Love it so far.

Everything works fine including mysql connection and sending emails. Except for one thing.

I keep getting the error Rendering: Template index does not exist.

File system:

├── /gogo
   ├── main
   ├── main.go
   ├── go.mod
   ├── go.sum
   ├── /views
        └── index.html
   └── /public
        └── plaatje.png
Copy after login

My main.go code:

package main

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

func main() {
    // initialize standard go html template engine
    template_engine := html.new(
        "./views",
        ".html",
    )

    // start fiber
    app := fiber.new(fiber.config{
        views: template_engine,
    })

    // add static folder
    app.static(
        "/static",  // mount address
        "./public", // path to the file folder
    )

    // endpoint
    app.get("/", func(c *fiber.ctx) error {
        // render index template
        return c.render("index", fiber.map{
            "title": "it works",
            "plat":  "almost",
        })
    })

    log.fatal(app.listen(":9990"))
}
Copy after login

index.html File:

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=Unicode">
    <title>{{.Title}}</title>
</head>
<body>
    <h1>{{.Title}}</h1>
    <p>{{.Plat}}</p>
    <p><img src="./static/plaatje.png"></p>
</body>
</html>
Copy after login

When I run it locally on my mac, everything works fine and the templates render as they should.

But on the ubuntu server, everything works fine except the templates and gives the given error:

Rendering: Template index does not exist

I tried changing ownership and permissions in ubuntu: no result. However, this is a bit of a blind spot for me, so this may still be an issue...

I tried modifying the view path (./views, /views, views.etc): no result.

I tried return c.render("index.html", fiber.map{: No result.

What did I miss?

Solution

Look for the error, it will appear above the fiber information box. For me it looked like this: 2023/03/12 15:40:58 [WARNING]: Unable to load view: template: apply: 9: Function 't' is undefined . If your template compiles, they will be found using relative paths.

The above is the detailed content of Golang Fiber Template Engine HTML: Rendering: Template does not exist. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template