What is the best way of creating loop in go html template?

huangapple go评论76阅读模式
英文:

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}}
    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/{{.}}&quot;&gt;{{.}}&lt;/a&gt;&lt;/div&gt;
{{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 &quot;html/template&quot;
import &quot;os&quot;

type data struct {
    Url   string
    Title string
}

type show struct {
    Pages []data
}

const html = `&lt;html&gt;
		    {{range .Pages}}
			    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/{{.Url}}&quot;&gt;{{.Title}}&lt;/a&gt;
&lt;/div&gt;
		{{end}}
		&lt;/html&gt;`

func show_template() {

    webpage, _ := template.New(&quot;template&quot;).Parse(html)

    mydata := []data{{
	Url:   &quot;page-1.html&quot;,
	Title: &quot;go to page 1&quot;,
}, {
	Url:   &quot;page-2.html&quot;,
	Title: &quot;go to page 2&quot;,
}, {
	Url:   &quot;page-3.html&quot;,
	Title: &quot;go to page 3&quot;,
}, {
	Url:   &quot;page-3.html&quot;,
	Title: &quot;go to page 3&quot;,
}}

web_data := show{mydata}

webpage.Execute(os.Stdout, web_data)

}

func main() {

	show_template()

}

and this is the result..

&lt;html&gt;

                    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/page-1.html&quot;&gt;go to page 1&lt;/a&gt;&lt;/div&gt;

                    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/page-2.html&quot;&gt;go to page 2&lt;/a&gt;&lt;/div&gt;

                    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/page-3.html&quot;&gt;go to page 3&lt;/a&gt;&lt;/div&gt;

                    &lt;div class=&quot;page&quot;&gt;&lt;a href=&quot;/posts/page-3.html&quot;&gt;go to page 3&lt;/a&gt;&lt;/div&gt;

                    &lt;/html&gt;

huangapple
  • 本文由 发表于 2014年9月26日 13:17:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/26052518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定