gofiber: 模板: 页面上用range循环显示

一,代码:

1, controller

func (dc *ArticleController) ListArticle(c *fiber.Ctx) error {
	// 处理获取文章的逻辑
	article1 := new(Article)
	article1.Id = 1
	article1.Title = "三国演义毛宗岗批本"
	article1.Author = "罗贯中"

	article2 := new(Article)
	article2.Id = 2
	article2.Title = "水浒传金圣叹批本"
	article2.Author = "施耐庵"
	
	articleList:=[]*Article{}

	articleList = append(articleList,article1)
	articleList = append(articleList,article2)

	myMap:=fiber.Map{}

	myMap["Title"] = "书店新上图书列表"
	myMap["ArticleList"] = articleList

	return c.Render("article/list", myMap)
}

2,list.html

{{template "partials/header" .}}
    <main style="background:#ffff00;width:100%;text-align: center;">
      <h1>{{.Title}}</h1>
 <table border="1">
     <tr>
     <td>key</td>
     <td>id</td>
     <td>标题</td>
     <td>作者</td>
     </tr>
     {{range $key, $value := .ArticleList}}
     <tr>
         <td>key: {{$key}}</td>
         <td>id: {{$value.Id}}</td>
         <td>title: {{$value.Title}}</td>
         <td>Author: {{$value.Author}}</td>
     </tr>
     {{end}}

 </table>
    </main>
{{template "partials/footer" .}}

 

二,测试效果:

 

posted @ 2024-11-17 17:27  刘宏缔的架构森林  阅读(10)  评论(0编辑  收藏  举报