英文:
Combining first function with index in golang/html template
问题
我正在使用Hugo创建一个博客。我想列出前三篇博客文章。到目前为止还没有问题。
{{ range first 3 .Data.Pages.ByPublishDate }}
但是我需要索引来添加CSS类。我用以下代码来实现:
{{ range $index, $element := .Data.Pages.ByPublishDate }}
现在我的问题是如何同时获取索引(就像第二行代码那样)并限制结果为3个。
不幸的是,这似乎不起作用。
{{ range first 3 $index, $element := .Data.Pages.ByPublishDate }}
有什么想法吗?
英文:
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
得分: 5
我认为根据你的示例,你正在寻找以下内容:
{{ range $index, $element := (first 3 .Data.Pages.ByPublishDate) }}
英文:
I think what you are looking for based on your examples is the following:
{{ range $index, $element := (first 3 .Data.Pages.ByPublishDate) }}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论