2012-12-31 20 views

cevap

12

ait Variables bölümü:

<table class="table table-striped table-hover" id="todolist"> 
     {{range $index, $results := .}}   
     <tr> 
      <td>{{add $index 1}}</td> 
      <td>{{.Title}}</td> 
      <td>{{.Description}}</td> 
      </tr> 
     {{end}} 
    </table> 

gitmek kodunda ben FuncMap geçirilen bir fonksiyon yazdım:

func add(x, y int) int { 
    return x + y 
} 
01 Benim işleyicisi olarak

:

type ToDo struct { 
    Id   int 
    Title  string 
    Description string 
} 

func IndexHandler(writer http.ResponseWriter, request *http.Request) { 
    results := []ToDo{ToDo{5323, "foo", "bar"}, ToDo{632, "foo", "bar"}} 
    funcs := template.FuncMap{"add": add} 
    temp := template.Must(template.New("index.html").Funcs(funcs).ParseFiles(templateDir + "/index.html")) 
    temp.Execute(writer, results) 
} 
9

Kontrol dışarı benim html template yılında text/template

http://golang.org/pkg/text/template/

range $index, $element := pipeline 
+0

html/template kullanmıyorum text/template kullanmıyor – jwesonga

+5

'html/template',' text/template' öğesine geçmeden önce değerleri çıkarır. "Html/template" dokümanlarına bakarsanız, sizi yalnızca "text/template" için dokümanlar aradığını fark edersiniz. – dskinner