Golang. 如何使用html/template包创建循环函数

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

Golang. How to create loop function using html/template package

问题

我正在尝试实现一个自定义函数作为循环。它接受迭代次数和花括号之间的内容,然后应该将花括号之间的内容迭代n次。请参考以下示例:

main.go

template.Must(template.ParseFiles("palette.html")).Funcs(template.FuncMap{
    "loop": func(n int, content string) string {
        var r string
        for i := 0; i <= n; i++ {
            r += content
        }
        return r
    },
}).ExecuteTemplate(rw, index, nil)

index.html

{{define "index"}}
<div class="row -flex palette">
  {{loop 16}}
    <div class="col-2"></div>
  {{end}}
</div>
{{end}}

输出

<div class="row -flex palette">
    <div class="col-2"></div>
    <div class="col-2"></div>
    <div class="col-2"></div>
    <div class="col-2"></div>
    ... 16次
</div>

是否可以实现这个功能?动机是因为text/template的标准功能不允许仅迭代花括号之间的内容。是的,我们可以通过使用range操作来遍历“外部”数据来实现。

英文:

I'm trying to implement loop as custom function. It takes number of iterations and content between curly brackets, then it should iterate content between brackets n times. Please, see example:

main.go

template.Must(template.ParseFiles(&quot;palette.html&quot;)).Funcs(template.FuncMap{
		&quot;loop&quot;: func(n int, content string) string {
			var r string
			for i := 0; i &lt;= n; i++ {
				r += content
			}
			return r
		},
	}).ExecuteTemplate(rw, index, nil)

index.html

{{define &quot;index&quot;}}
&lt;div class=&quot;row -flex palette&quot;&gt;
  {{loop 16}}
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
  {{end}}
&lt;/div&gt;
{{end}}

Output

&lt;div class=&quot;row -flex palette&quot;&gt;
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
    ... 16 times
&lt;/div&gt;

Is it possible to implement it? The motivation is that standard functionality of the text/template doesn't allow just to iterate content between curly bracket. Yes we can do it by range action going through "outside" data.

答案1

得分: 12

你可以在返回切片的函数上使用rangehttp://play.golang.org/p/FCuLkEHaZn

package main

import (
	"html/template"
	"os"
)

func main() {
	html := `
<div class="row -flex palette">
  {{range loop 16}}
    <div class="col-2"></div>
  {{end}}
</div>`
	tmpl := template.Must(template.New("test").Funcs(template.FuncMap{
		"loop": func(n int) []struct{} {
			return make([]struct{}, n)
		},
	}).Parse(html))
	tmpl.Execute(os.Stdout, nil)
}
英文:

You can use range on a function that returns a slice. http://play.golang.org/p/FCuLkEHaZn

package main

import (
	&quot;html/template&quot;
	&quot;os&quot;
)

func main() {
	html := `
&lt;div class=&quot;row -flex palette&quot;&gt;
  {{range loop 16}}
    &lt;div class=&quot;col-2&quot;&gt;&lt;/div&gt;
  {{end}}
&lt;/div&gt;`
	tmpl := template.Must(template.New(&quot;test&quot;).Funcs(template.FuncMap{
		&quot;loop&quot;: func(n int) []struct{} {
			return make([]struct{}, n)
		},
	}).Parse(html))
	tmpl.Execute(os.Stdout, nil)
}

huangapple
  • 本文由 发表于 2015年3月8日 01:06:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/28917530.html
匿名

发表评论

匿名网友

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

确定