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

huangapple go评论176阅读模式
英文:

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 (
	&quot;fmt&quot;
	&quot;log&quot;
    fiber &quot;github.com/gofiber/fiber/v2&quot;
    &quot;github.com/gofiber/template/html&quot;
)

func main() {
	// Initialize standard Go html template engine
	template_engine := html.New(
		&quot;./views&quot;,
		&quot;.html&quot;,
	)

    // start fiber
	app := fiber.New(fiber.Config{
		Views: template_engine,
	})

    // add static folder
	app.Static(
		&quot;/static&quot;,  // mount address
		&quot;./public&quot;, // path to the file folder
	)

    // endpoint
	app.Get(&quot;/&quot;, func(c *fiber.Ctx) error {
		// Render index template
		return c.Render(&quot;index&quot;, fiber.Map{
			&quot;Title&quot;: &quot;It works&quot;,
			&quot;Plat&quot;:  &quot;almost&quot;,
		})
	})

	log.Fatal(app.Listen(&quot;:9990&quot;))
}

The index.html file:

&lt;!DOCTYPE html&gt;
&lt;head&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=Unicode&quot;&gt;
    &lt;title&gt;{{.Title}}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;{{.Title}}&lt;/h1&gt;
    &lt;p&gt;{{.Plat}}&lt;/p&gt;
    &lt;p&gt;&lt;img src=&quot;./static/plaatje.png&quot;&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;

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(&quot;index.html&quot;, 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 &quot;t&quot; 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(
        &quot;./views&quot;,
        &quot;.html&quot;,
    )

... with a relative path to an absolute path ...

    template_engine := html.New(
        &quot;/home/username/go/views&quot;,
        &quot;.html&quot;,
    )

... solved the issue.

This issue is not mentioned in any source on this topic.

huangapple
  • 本文由 发表于 2022年12月6日 18:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/74700974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定