dqwyghl0649 2016-01-25 14:40 采纳率: 0%
浏览 42
已采纳

在golang / html模板中将第一个函数与索引结合

I'm creating a blog with Hugo. i would like to list the first 3 Blog entries. That is not a problem so far.

{{ range first 3 .Data.Pages.ByPublishDate }}

But i need the index for adding css classes. I do that with this line

{{ range $index, $element := .Data.Pages.ByPublishDate }}

My problem now is how following. How do I get the index like in the second line of code but still limit the result to 3.

Unfortunately this doesn't seem to work.

{{ range first 3 $index, $element := .Data.Pages.ByPublishDate }}

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongou1970 2016-01-25 14:55
    关注

    I think what you are looking for based on your examples is the following:

    {{ range $index, $element := (first 3 .Data.Pages.ByPublishDate) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决