Gofiber框架模板的问题

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

Issue with Gofiber framework template

问题

我正在学习GO语言中的Fiber框架,并且在解决模板引擎返回错误的问题时遇到了困难。当body部分是一个include时,以下代码可以正常工作,但是当我添加另一个include用于"body section"时,就会抛出错误:

错误信息:

html/template:fun: """"在属性名中:"" &quot;>Read Full Article</a>\n ""


这个可以正常工作:

Gofiber框架模板的问题

我无法在body内容中添加另一个"partial"模板,我甚至尝试在这个部分中使用完整的设计HTML(不使用include),但无论哪种方式,加载时都会抛出相同的错误。不知何故,上面的示例body可以正常工作,但错误信息并没有给我提供太多信息。

这个无法工作:

Gofiber框架模板的问题

这个也不工作:

{{template &quot;includes/header&quot; .}}
{{template &quot;includes/navigation&quot; .}}
// full html body text here  (much longer than first working example)
{{template &quot;includes/footer&quot; .}}
英文:

I am in the process of learning fiber framework in GO language and having trouble figure out why the template engine is returning an error when the body section is an include. The following works as expected but when I add in another include for the "body section" it throws an error:

The Error:

html/template:fun: "&quot;" in attribute name: " &quot;>Read Full Article</a>\n "


This one works:

Gofiber框架模板的问题

I am unable to add another template "partial" in the middle for body content, I have even tried the full design html in this section (without using includes), either way it throws the same error when loading. For some reason this sample body above works fine, but the error isn't telling me much.

This won't work:

Gofiber框架模板的问题

Nor does this work:

{{template &quot;includes/header&quot; .}}
{{template &quot;includes/navigation&quot; .}}
// full html body text here  (much longer than first working example)
{{template &quot;includes/footer&quot; .}}

答案1

得分: 0

我不确定为什么标准模板引擎出现问题,因为错误信息不太清楚,但我通过切换到Jet引擎来解决了这个问题。Jet似乎具有更好的错误处理能力。所有模板扩展名必须切换为.jet扩展名,并且包含部分的语法稍有变化,变成{{include "directory/file_name"}}。

// 加载模板
engine := jet.New("./views", ".jet")

// 启动Fiber
app := fiber.New(fiber.Config{
Views: engine,
})

英文:

I am not sure why the standard template engine had issues because the errors were not very clear, but I was able to fix the issue by switching over the jet engine. Jet seems to have better error handling. All template extensions must be switched to .jet extension and the includes change a little bit to {{include "directory/file_name" }}

// Loading Templates
engine := jet.New(&quot;./views&quot;, &quot;.jet&quot;)

// Start Fiber
app := fiber.New(fiber.Config{
	Views: engine,
})

huangapple
  • 本文由 发表于 2022年12月28日 04:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/74933892.html
匿名

发表评论

匿名网友

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

确定