我的问题如标题所述。我正在尝试做类似的事情:
{{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}}
我收到错误:
template: products.html:9: unexpected "%" in operand
是否有其他方法可以在模板中进行模数计算?
Add a 模板函数 https://godoc.org/text/template#Template.Funcs与你需要的逻辑。例如:
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}} ...`)
游乐场示例 http://play.golang.org/p/IT6jrQGOze
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)