Golang Fiber的c.Render布局未正确执行模板。

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

Golang fiber c.Render layout not excute right template

问题

我正在尝试使用一个布局和不同的模板。在layout.html中,我有以下内容:

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>从布局加载</h1>
  {{ template . }}
</body>
</html>

dashboard.html中,我有以下内容:

{{ define "dashboard" }}
    <h1>仪表盘</h1>
{{ end }}

login.html中,我有以下内容:

{{ define "login" }}
    <h1>登录</h1>
{{ end }}

当我通过控制器渲染仪表盘时,它只加载login.html,它总是加载最后一个HTML文件:

// 控制器

func Dashboard(c *fiber.Ctx) error {
	return c.Render("dashboard", fiber.Map{
		"title": "登录 | Selectify Admin",
	}, "partials/layout")
}

在主文件中,我告诉它在哪里找到视图:

// main.go

// 创建一个新的HTML引擎
template_engine := html.New(
	"./views",
	".html",
)

// 创建一个新的Fiber应用程序
app := fiber.New(fiber.Config{
	Views: template_engine, // 设置视图引擎

	ErrorHandler: func(c *fiber.Ctx, err error) error {
		return utils.HandleError(c, err)
	},
})

它会报错:"template: "partials/layout" 是一个不完整或空的模板"。如何使用一个布局发送或渲染正确的HTML文件?

英文:

I'm trying to use one layout with different templates,

├── Main Folder
   ├── cmd
        └── main.go
   ├── controllers
   ├── models
   ├── views
        └── partials
            └── layout.html
        └── index.html
        └── dashboard.html
        └── login.html
   └── public
        └── sample.png

In my layout.html, I have

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Loaded from Layout&lt;/h1&gt;
  {{ template . }}
&lt;/body&gt;
&lt;/html&gt;

In my dashboard.html

{{ define &quot;dashboard&quot; }}
    &lt;h1&gt;Dashboard&lt;/h1&gt;
{{ end }}

In my login.html

{{ define &quot;login&quot; }}
    &lt;h1&gt;Login&lt;/h1&gt;
{{ end }}

When I'm render dashboard through controller its only load login.html, its take last html file always

// controller

func Dashboard(c *fiber.Ctx) error {
	return c.Render(&quot;dashboard&quot;, fiber.Map{
		&quot;title&quot;: &quot;Login | Selectify Admin&quot;,
	}, &quot;partials/layout&quot;)
}

In main file I have told where to find views

//main.go

// create a new HTML engine
	template_engine := html.New(
		&quot;./views&quot;,
		&quot;.html&quot;,
	)

	// create a new Fiber app
	app := fiber.New(fiber.Config{
		Views: template_engine, // set the views engine

		ErrorHandler: func(c *fiber.Ctx, err error) error {
			return utils.HandleError(c, err)
		},
	})

it's giving an error
"template: "partials/layout" is an incomplete or empty template"
How can I send or render correct html file using one layout??

Go version 1.19
fiver version 2
"github.com/gofiber/template/html"

答案1

得分: 2

找到答案了,我们需要使用{{embed}}而不是template,这样它将包含你从控制器传递的正确模板。

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>从布局加载</h1>
  {{ embed }}
</body>
</html>
英文:

Found the answer,
instead of template we gotta use {{embed}}, then it'll include right temlate which you pass from controller


&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Loaded from Layout&lt;/h1&gt;
  {{ embed  }}
&lt;/body&gt;
&lt;/html&gt;

huangapple
  • 本文由 发表于 2023年1月6日 15:03:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027921.html
匿名

发表评论

匿名网友

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

确定