英文:
What is the best way of creating loop in go html template?
问题
我正在尝试使用html/template
Go包创建一个用于显示帖子的HTML模板。
我还想在页面上进行分页,每页显示5篇帖子。
所以我从我的帖子存储库中获取帖子数量,将其除以每页帖子数并四舍五入(向上取整)。这就是当前可用的帖子总页数。
我将总页数传递给我的HTML模板。
现在,在我的HTML模板中,我需要显示从1到总页数的页面按钮。
在text/html
包中,有关如何使用管道的文档非常棒,但我没有找到创建简单循环的任何示例。
我找到了解决方案,但我不确定它是否是最好的。
我可以将可用页面的数组传递给模板,这样在模板中我可以做如下操作:
{{range .pages}}
<div class="page"><a href="/posts/{{.}}">{{.}}</a></div>
{{end}}
但也许有比传递页面数组更好的方法吗?
我也知道可以将自定义函数传递给模板。这可能是一个解决方案吗?
英文:
I'm trying to create a html template for displaying posts via html/template
Go package.
I also want to make pagination on my page, to display 5 posts per page.
So I take the post count from my post repository, dividing it by posts per page value and rounding it (ceil). That's the total number of pages with posts currently available.
I pass the total number of pages to my html template.
Now, in my html template I need to display page buttons from 1 to the total number.
In the text/html
package there is an awesome documentation about how to work with pipelines, but I didn't find any example of creating simple loop.
I got the solution, but I am not sure it is the good one.
I can pass to template not just the total number of pages, but an array of available pages, so in my template I can do something like:
{{range .pages}}
<div class="page"><a href="/posts/{{.}}">{{.}}</a></div>
{{end}}
But maybe there is a better way to do this than passing an array of pages?
I also know about possibility of passing custom functions to template. Could it be a solution?
答案1
得分: 1
规则是模板必须包含尽可能少的逻辑(这就是为什么原生函数和控件在模板包中非常有限的原因)。
你应该通过将数据放入一个专用的结构体(以便传递给模板)来准备你的数据。然后,你可以使用range函数将这个由变量和数组组成的结构体显示在模板中,就像你打算做的那样。
英文:
The rule is that the template must contain the minimal logic possible (and that's the reason why the native functions and controls are so limited into the template package).
You should prepare your data into the controller by putting it into a dedicated struct (to be passed to the template). Then you can display this struct (composed of variables and arrays) into the template by using the range function as you intended to do.
答案2
得分: 1
请稍等,我会为您进行翻译。
英文:
try this, i have do my best...
package main
import "html/template"
import "os"
type data struct {
Url string
Title string
}
type show struct {
Pages []data
}
const html = `<html>
{{range .Pages}}
<div class="page"><a href="/posts/{{.Url}}">{{.Title}}</a>
</div>
{{end}}
</html>`
func show_template() {
webpage, _ := template.New("template").Parse(html)
mydata := []data{{
Url: "page-1.html",
Title: "go to page 1",
}, {
Url: "page-2.html",
Title: "go to page 2",
}, {
Url: "page-3.html",
Title: "go to page 3",
}, {
Url: "page-3.html",
Title: "go to page 3",
}}
web_data := show{mydata}
webpage.Execute(os.Stdout, web_data)
}
func main() {
show_template()
}
and this is the result..
<html>
<div class="page"><a href="/posts/page-1.html">go to page 1</a></div>
<div class="page"><a href="/posts/page-2.html">go to page 2</a></div>
<div class="page"><a href="/posts/page-3.html">go to page 3</a></div>
<div class="page"><a href="/posts/page-3.html">go to page 3</a></div>
</html>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论