首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >golang iris直接返回模板html

golang iris直接返回模板html
EN

Stack Overflow用户
提问于 2018-06-29 18:18:06
回答 1查看 527关注 0票数 -1
代码语言:javascript
复制
<!-- file: web/views/hello/index.html -->
<html>
<head>
    <title>{{.Title}} - My App</title>
</head>
<body>
    <p>{{.MyMessage}}</p>
</body>
</html>

代码语言:javascript
复制
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,
    )
}

代码语言:javascript
复制
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}}

EN

回答 1

Stack Overflow用户

发布于 2018-07-06 04:58:36

以下是我从mvc/overview example得到的结果,不出所料:

有什么能帮到你的吗?

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51099508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档