我有一个Gin Web应用程序,基于一组分词和一个基本模板的多个HTML模板.基本模板似乎与相关的部分一起呈现得很好,但我的主视图、登录、索引和注册并没有像预期的那样呈现.每当我访问其中任何一个的HTTP端点时,只有注册视图呈现.

以下文件中缺少什么或配置错误,导致我的路由无法呈现所请求的页面?

我的项目有以下 struct .

├── app
...
│   ├── handlers
│   │   ├── general
│   │   │   └── general.go
│   │   └── routes.go
│   ├── main.go
│   ├── reloadDev.sh
│   ├── static
│   │   ├── css
│   │   ├── img
│   │   └── js
│   └── templates
│       ├── home
│       │   ├── index.tmpl.html
│       │   ├── login.tmpl.html
│       │   └── 注册.tmpl.html
│       ├── layouts
│       │   └── Bas.tmpl.嗯!
│       └── partials
│           ├── footer.tmpl.html
│           ├── head.tmpl.html
│           └── navbar.tmpl.html

Bas.tmpl.嗯!

{{ define "base" }}
<!DOCTYPE html>
<html lang="eng" data-bs-theme="dark">
    {{ template "head" . }}
    {{template "navbar" .}}
    <body>
    {{ block "content" . }}{{ end }}
    </body>
    {{template "footer" .}}
</html>
{{end}}

注册.tmpl.html

{{ template "base" . }}
{{ define "content" }}
<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <h1>Register</h1>
            <form action="/register" method="post">
                <div class="mb-3">
                    <label for="username" class="form-label">Username</label>
                    <input type="text" name="username" id="username" class="form-control" placeholder="Username" required>
                </div>
                <div class="mb-3">
                    <label for="password" class="form-label">Password</label>
                    <input type="password" name="password" id="password" class="form-control" placeholder="Password" required>
                </div>
...SNIP...
                <button type="submit" class="btn btn-primary">Register</button>
            </form>
        </div>
    </div>
</div>
{{ end }}

Index.tmpl.html(Login的 struct 与这两者相同.)

{{ template "base" . }}
{{ define "title" }}Home{{ end }}
{{ define "content" }}
<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <p>Welcome to Astra Porta.</p>
            <p>Click <a href="/login">here</a> to login.</p>
        </div>
    </div>
</div>
{{ end }}

使用embed.FS将HTML模板与二进制文件Bundle 在一起.

//go:embed templates/partials/* templates/layouts/* templates/home/*
var files embed.FS

func main() {
    router := setupRouter()
    err := router.Run()
    if err != nil {
        panic(err)
    }
}

func setupRouter() *gin.Engine {
    router := gin.Default()
    subFS, e := fs.Sub(files, "templates")
    if e != nil {
        panic(e)
    }

tmpl := template.Must(template.ParseFS(
    subFS,
    "layouts/*.html",
    "partials/*.html",
    "home/*.html",
))
router.SetHTMLTemplate(tmpl)

router.StaticFS("/static", http.Dir("static"))

err := router.SetTrustedProxies(nil)
if err != nil {
    panic(err)
}
handlers.InitializeRoutes(&router.RouterGroup)
return router
}

并且页面在我的应用程序路径内呈现.这里的引用映射到这*.tmpl.html个文件的文件名.

func SiteIndex(c *gin.Context) {
    c.HTML(http.StatusOK, "index.tmpl.html", nil)
}

func GetRegister(c *gin.Context) {
    c.HTML(http.StatusOK, "注册.tmpl.html", nil)
}

func GetLogin(c *gin.Context) {
    c.HTML(http.StatusOK, "login.tmpl.html", nil)
}

推荐答案

对于任何其他有这个问题的人.Mkopriva comments 中提到的解决方案是正确的.我删除了base.tmpl.html个,并使用更新的部分和目标页面组合了每个视图.

报头

{{ define "报头" }}
<!DOCTYPE html>
<html lang="eng" data-bs-theme="dark">
{{template "navbar" .}}
    <body>
    {{ block "content" . }}{{ end }}
        <head><meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
           ...SNIP...
            <title>App</title>
        </head>
{{end}}

页脚

{{define "页脚"}}

        <div class="container">
            <页脚 class="py-3 my-4" data-bs-theme="dark">
                <ul class="nav justify-content-center border-bottom pb-3 mb-3">
                    <li class="nav-item"><a href="/" class="nav-link px-2 text-body-secondary">Home</a></li>
                </ul>
                <p class="text-center text-body-secondary">© 2024 .</p>
            </页脚>
        </div>
    </body>
</html>
{{end}}

相关页面

{{template "报头"}}
<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3">
            <p>Welcome to Astra Porta.</p>
            <p>Click <a href="/login">here</a> to login.</p>
        </div>
    </div>
</div>
{{template "页脚"}}

Go相关问答推荐

Gorm foreign 密钥

Zitadel示例Go Webapp加密密钥

为什么 `go mod` 占用了另一个磁盘上的空间而不是我的 GOPATH?

Golang:如何在不转义每个动作的情况下呈现模板的模板?

从单词中删除特殊字符

如何在 `hashicorp / terraform-exec` 中将 `ApplyConfig` 传递给 `tf.Apply()`?

如何从 Asterisk Manager Interface Event 获取活动呼叫数

Wire google Inject with multi return from provider 函数

如何在切片增长时自动将切片的新元素添加到函数参数

Golang - 客户 Unmarshaler/Marshaler 在指针上具有 nil/null 值

在 Go 中发送 ack 和 term 后消息仍在 nats 限制队列中

Golang Getrlimit 返回与 ulimit 不同的值

每次有人进入我的网站时如何运行特定功能?

go:识别重新定义标志的包

在 Raspberry Pi4 上下载 Go Mod

Golang grpc go.mod 问题

panic :拨号 tcp:在 172.22.64.1:53 上查找 bookstoreDB:没有这样的主机

如何在 docker 文件中安装 golang 包?

处理程序中的无限循环

Gin中测试模式有什么用