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.
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
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")) }
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>
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?
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!