====== go:templating:go_html_template_range ====== Go html/template (range loop) Note: html/template is a Go (Golang) standard library. The idea of “looping in templates” is similar to PHP templating engines (Blade/Twig). ===== What it is ===== In Go html/template, the action used to loop over a slice/array/map is: {{range .}} ... {{end}} range iterates over the data (e.g., a slice) and renders the inside block once per element. . (dot) is the “current context”. Inside range, . becomes the current item. ===== What it’s for ===== Render lists in HTML: menus, table rows, cards, comments, products, etc. Iterate over a map to output key/value. Handle empty lists using else. ===== Examples ===== 1) Simple slice loop {{range .}}
Name: {{.Name}} | Email: {{.Email}}
{{end}} 4) Empty list handling (else) {{range .}}