gpt4 book ai didi

golang iris直接返回模板html

转载 作者:数据小太阳 更新时间:2023-10-29 03:37:53 25 4
gpt4 key购买 nike

<!-- file: web/views/hello/index.html -->
<html>
<head>
<title>{{.Title}} - My App</title>
</head>
<body>
<p>{{.MyMessage}}</p>
</body>
</html>

package main

import (
"github.com/kataras/iris"
)
func main() {
app := iris.New()
// Load the template files.
app.RegisterView(iris.HTML("./web/views", ".html"))
// Serve our controllers.
mvc.New(app.Party("/hello")).Handle(new(controllers.HelloController))
// http://localhost:8080/hello
app.Run(
iris.Addr("localhost:8080"),
iris.WithoutVersionChecker,
iris.WithoutServerError(iris.ErrServerClosed),
iris.WithOptimizations,
)
}

package controllers

import (
"github.com/kataras/iris/mvc"
)
type HelloController struct{}

var helloView = mvc.View{
Name: "hello/index.html",
Data: map[string]interface{}{
"Title": "Hello Page",
//"MyMessage": "Welcome to my awesome website",
},
}
func (c *HelloController) Get() mvc.Result {
return helloView
}

我想直接返回模板html。在 func (c *HelloController) Get() mvc.View { return helloView } 中,但是一旦我这样做了,模板中的参数就会为空。 {{.Title}} 和 {{.MyMessage}} 丢失

最佳答案

这是我在 mvc/overview example 中的结果,正如预期的那样: enter image description here

有什么可以帮您的吗?

关于golang iris直接返回模板html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51099508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com