Home > Backend Development > Golang > Golang Fiber c. Rendering layout does not enforce correct templates

Golang Fiber c. Rendering layout does not enforce correct templates

WBOY
Release: 2024-02-09 13:54:18
forward
1073 people have browsed it

Golang Fiber c.渲染布局未执行正确的模板

Golang Fiber is a high-performance web framework that is widely used in Go language development. However, sometimes when using Fiber to render a layout, you may encounter problems where the template is not executed correctly. This may be due to a bug in the code or a configuration issue. In this article, PHP editor Apple will analyze this problem in detail and provide solutions to help you overcome this challenge. Whether you are a beginner or an experienced developer, you can get useful knowledge and skills from this article to improve your capabilities in Golang Fiber development.

Question content

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

├── main folder
   ├── cmd
        └── main.go
   ├── controllers
   ├── models
   ├── views
        └── partials
            └── layout.html
        └── index.html
        └── dashboard.html
        └── login.html
   └── public
        └── sample.png
Copy after login

In my layout.html I have

<!doctype html>
<html lang="en">
<head>
</head>
<body>
    <h1>loaded from layout</h1>
  {{ template . }}
</body>
</html>
Copy after login

In my dashboard.html

{{ define "dashboard" }}
    <h1>dashboard</h1>
{{ end }}
Copy after login

In my login.html

{{ define "login" }}
    <h1>login</h1>
{{ end }}
Copy after login

When I render the dashboard through the controller, it only loads login.html, it always gets the last html file

// controller

func dashboard(c *fiber.ctx) error {
    return c.render("dashboard", fiber.map{
        "title": "login | selectify admin",
    }, "partials/layout")
}
Copy after login

In the main file I have told where to find the view

//main.go

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

    // 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)
        },
    })
Copy after login

An error occurred "Template: 'partials/layout' is an incomplete or empty template" How to send or render correct html file using one layout?

go version 1.19 fifth edition 2 "github.com/go fiber/template/html"

Solution

Find the answer, We have to use {{embed}} instead of template, then it will contain the correct template that you pass from controller

<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
    <h1>Loaded from Layout</h1>
  {{ embed  }}
</body>
</html>
Copy after login

The above is the detailed content of Golang Fiber c. Rendering layout does not enforce correct templates. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template