首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Go Fiber应用程序中呈现模板

在Go Fiber应用程序中呈现模板
EN

Stack Overflow用户
提问于 2021-04-14 01:25:52
回答 1查看 438关注 0票数 0

有没有人有很好的gofiber和模板的例子?

我正在尝试通过go fiber模板显示视频列表

下面是我的Go代码(实体):

代码语言:javascript
复制
type Video struct {
    Id          int    `json:"id"`
    Title       string `json:"title"`
    Description string `json:"description"`
    VideoUrl    string `json:"description"`
    Link        string `json:"description"`
}

var videos = []*Video{
    {
        Id:          1,
        Title:       "Podcast #1",
        Description: "Here I discuss about stuff",
        VideoUrl:    "foo.m3u8",
        Link:        "videos/foo",
    },
    {
        Id:          2,
        Title:       "Podcast #2",
        Description: "Still talking",
        VideoUrl:    "bar.m3u8",
        Link:        "videos/bar",
    },
}

下面是我的Go代码(控制器):

代码语言:javascript
复制
func main() {
    // Fiber instance
    app := fiber.New(fiber.Config{
        Views: html.New("./views", ".html"),
    })

    // Serve static assets
    app.Static("/", "./public", fiber.Static{
        Compress: true,
    })

    // Routes
    app.Get("/", index)

    // Start server
    log.Fatal(app.Listen(":3000"))
}

// Handler
func index(c *fiber.Ctx) error {
    return c.Render("index", fiber.Map{
        "Videos": videos,
    })
}

下面是我的伪HTML:

代码语言:javascript
复制
<div class="col-lg-4">
    <video-js id={{.Id}} class="vjs-default-skin my-tuby-video" controls preload="auto" width="640" height="268">
    <source src="{{.VideoUrl}}" type="application/x-mpegURL">
    </video-js>
    <script>
    var player = videojs({{.Id}});
    </script>
    
    <h2>{{.Title}}</h2>
    <p>{{.Description}}</p>
    <p><a class="btn btn-secondary" href="#">{{.Link}}</a></p>
</div><!-- /.col-lg-4 -->

另外,我如何为我列表中的每个视频重复这一位HTML?

提前感谢您的任何意见。

EN

回答 1

Stack Overflow用户

发布于 2021-04-14 03:20:55

正如Cerise Limon评论的那样:

代码语言:javascript
复制
{{range .Videos}}
<div class="col-lg-4">
    <video-js id={{.Id}} class="vjs-default-skin my-tuby-video" controls preload="auto" width="640" height="268">
    <source src="{{.VideoUrl}}" type="application/x-mpegURL">
    </video-js>
    <script>
    var player = videojs({{.Id}});
    </script>
    
    <h2>{{.Title}}</h2>
    <p>{{.Description}}</p>
    <p><a class="btn btn-secondary" href="#">{{.Link}}</a></p>
</div><!-- /.col-lg-4 -->
{{end}}

成功了。

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

https://stackoverflow.com/questions/67079636

复制
相关文章

相似问题

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