英文:
Golang Fiber template engine HTML: render: template does not exist
问题
在我的Ubuntu 22.10 DigitalOcean服务器上,我正在尝试使用Golang、Fiber和html模板引擎进行实验。到目前为止,我非常喜欢它。
所有的东西都能正常工作,包括Mysql连接和发送电子邮件。除了一个问题。
我一直收到错误消息render: template index does not exist。
文件系统:
├── /gogo
├── main
├── main.go
├── go.mod
├── go.sum
├── /views
└── index.html
└── /public
└── plaatje.png
我的main.go代码:
package main
import (
"fmt"
"log"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html"
)
func main() {
// 初始化标准的Go html模板引擎
template_engine := html.New(
"./views",
".html",
)
// 启动Fiber
app := fiber.New(fiber.Config{
Views: template_engine,
})
// 添加静态文件夹
app.Static(
"/static", // 挂载地址
"./public", // 文件夹路径
)
// 端点
app.Get("/", func(c *fiber.Ctx) error {
// 渲染index模板
return c.Render("index", fiber.Map{
"Title": "It works",
"Plat": "almost",
})
})
log.Fatal(app.Listen(":9990"))
}
index.html文件:
<!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>
当我在我的Mac上本地运行它时,一切正常,模板被正确渲染。
但是在Ubuntu服务器上,除了模板之外,其他都正常工作,并显示了给定的错误:
render: template index does not exist
我尝试在Ubuntu上更改所有权和权限,但没有结果。然而,这对我来说还是一个盲点,所以可能仍然是问题所在...
我尝试过调整视图路径(./views、/views、views等),但没有结果。
我尝试过return c.Render("index.html", fiber.Map{
,但没有结果。
我漏掉了什么?
英文:
On my Ubuntu 22.10 digitalocean server, I'm experimenting with Golang and Fiber and the html template engine. Loving it so far.
It all works, including the Mysql connection and sending email. Except for one thing.
I keep getting the error render: template index does not exist.
File system:
├── /gogo
├── main
├── main.go
├── go.mod
├── go.sum
├── /views
└── index.html
└── /public
└── plaatje.png
The code of my main.go:
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"))
}
The 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, it all works and the template is rendered as it should.
But on the Ubuntu server it all works except for the template, with the given error:
render: template index does not exist
I've tried changing ownership and permissions in Ubuntu: no results. However this is a bit of a blind spot for me, so this might still be the isue...
I've tried tinkering with the views path (./views, /views, views. etc): no results.
I've tried return c.Render("index.html", fiber.Map{
: no results.
What am I missing?
答案1
得分: 1
寻找错误,它将出现在Fiber信息框的上方。对我来说,错误是这样的:2023/03/12 15:40:58 [警告]: 无法加载视图:模板:apply:9:函数“t”未定义
。如果你的模板编译成功,它们将会被使用相对路径找到。
英文:
Look for an error, it will come out above the box of Fiber info. For me it was this: 2023/03/12 15:40:58 [Warning]: failed to load views: template: apply:9: function "t" not defined
. If your templates compile, they will be found using the relative path.
答案2
得分: 0
使用go run .
进行实验给了一个线索。当将其作为服务运行时,挂载点不是main所在的目录,而是服务器上的其他路径。
将代码中的...
template_engine := html.New(
"./views",
".html",
)
...改为相对路径的绝对路径...
template_engine := html.New(
"/home/username/go/views",
".html",
)
...解决了这个问题。
这个问题在任何关于这个主题的资料中都没有提到。
英文:
The experiment with go run .
gave a clue. When running it as a service, the mount point is not the dir where main is, but a path elsewhere on the server.
Changing...
template_engine := html.New(
"./views",
".html",
)
... with a relative path to an absolute path ...
template_engine := html.New(
"/home/username/go/views",
".html",
)
... solved the issue.
This issue is not mentioned in any source on this topic.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论