2016-04-02 14 views
3

私の質問はタイトルに記載されています。Go:テンプレート内で使用できるモジュラスはありますか?

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

テンプレートでモジュラスを行うための別の方法があります:私はエラーを取得する

{{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}} 

:私のような何かをしようとしていますか?

答えて

9

template functionに必要なロジックを追加します。たとえば、次のように

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}} ...`) 

playground example