Go:我可以在模板中使用模数吗?

Nic*_*cci 6 go

我的问题如标题所述.我想做的事情如下:

{{range $index, $element := .Products}}
    {{if $index % 4 == 0}}<div class="row">{{end}}
        <div class="columns small-3 product">
            <img src="/img/{{.ImageUrl}}" alt="{{.ImageUrl}}" />
                <a href="/product">
                    <h3>{{.Title}}</h3>
                </a>
                <p>
                  {{.Description}}
                </p>
                <p>
                    {{.Price}} / liter
                </p>
                </div>
        {{if index % 4 == 0}}</div>{{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

template: products.html:9: unexpected "%" in operand

是否有另一种方法可以在模板中进行模数?

Cer*_*món 13

使用您需要的逻辑添加模板功能.例如:

t := template.New("")
t.Funcs(template.FuncMap{"mod": func(i, j int) bool { return i%j == 0 }})
t.Parse(`... {{if mod $index 4}}<div class="row">{{{end}} ...`)
Run Code Online (Sandbox Code Playgroud)

操场的例子