<!-- 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 { helloView }中,但是一旦我这样做了,模板中的参数就会为空。缺少{{.Title}}和{{.MyMessage}}
发布于 2018-07-06 04:58:36
https://stackoverflow.com/questions/51099508
复制相似问题