在Golang开发中,模板引擎是非常重要的一部分,它能够帮助开发者更方便地渲染HTML页面。而Fiber作为Golang的轻量级Web框架,也提供了自己的模板引擎。在使用Fiber的过程中,有时候会遇到"模板不存在"的问题,这个问题可能是由于路径设置不正确或者文件确实不存在所导致。在本文中,php小编柚子将为大家详细解析这个问题的原因,并给出解决方案,帮助大家更好地使用Fiber模板引擎进行HTML渲染。
问题内容
在我的 ubuntu 22.10 digitalocean 服务器上,我正在尝试使用 golang 和 fiber 以及 html 模板引擎。到目前为止很喜欢它。
一切正常,包括 mysql 连接和发送电子邮件。除了一件事。
我不断收到错误渲染:模板索引不存在。
文件系统:
├── /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() { // 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 文件:
<!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Unicode"> <title>{{.Title}}</title> </head> <body> <h1 id="Title">{{.Title}}</h1> <p>{{.Plat}}</p> <p><img src="/static/imghwm/default1.png" data-src="./static/plaatje.png" class="lazy" alt="Golang Fiber 模板引擎 HTML:渲染:模板不存在" ></p> </body> </html>
当我在 mac 上本地运行它时,一切正常,并且模板按其应有的方式呈现。
但是在 ubuntu 服务器上,除了模板之外,一切正常,并出现给定的错误:
渲染:模板索引不存在
我尝试在 ubuntu 中更改所有权和权限:没有结果。然而,这对我来说有点盲点,所以这可能仍然是问题......
我尝试修改视图路径(./views、/views、views.etc):没有结果。
我尝试过 return c.render("index.html", fiber.map{
: 没有结果。
我错过了什么?
解决方法
查找错误,它会出现在光纤信息框上方。对我来说是这样的:2023/03/12 15:40:58 [警告]:无法加载视图:模板:apply:9:函数“t”未定义
。如果您的模板编译,将使用相对路径找到它们。
以上是Golang Fiber 模板引擎 HTML:渲染:模板不存在的详细内容。更多信息请关注PHP中文网其他相关文章!

goroutinesarefunctionsormethodsthatruncurranceingo,启用效率和灯威量。1)shememanagedbodo'sruntimemultimusingmultiplexing,允许千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函数可以从panic中恢复。具体方法是:1)在defer函数中使用recover()捕获panic,避免程序崩溃;2)记录详细的错误信息以便调试;3)根据具体情况决定是否恢复程序执行;4)谨慎使用,以免影响性能。

本文讨论了使用GO的“字符串”软件包进行字符串操作,详细介绍了共同的功能和最佳实践,以提高效率并有效地处理Unicode。

本文详细介绍了GO的“时间”包用于处理日期,时间和时区,包括获得当前时间,创建特定时间,解析字符串以及测量经过的时间。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

SublimeText3汉化版
中文版,非常好用

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver Mac版
视觉化网页开发工具