49 lines
1.4 KiB
Plaintext
49 lines
1.4 KiB
Plaintext
Defining template:
|
|
{{ define “template_name” }} {{ end }}
|
|
|
|
Using template:
|
|
{{ template “template_name” . }}
|
|
|
|
Block:
|
|
{{ block “content” . }} {{ end }}
|
|
|
|
Is equivalent to => {{ template “content” . }} {{ define “content” }} {{ end }}
|
|
|
|
If:
|
|
{{ if var }} //… {{ else if var }} //… {{ else }} //… {{ end }}
|
|
|
|
Assignment:
|
|
{{ $first_name := “arash” }} {{ $first_name = .Name }}
|
|
|
|
With:
|
|
{{ with . Firstname }} // . refers to Firstname {{ end }}
|
|
|
|
{{ with $ firstname := .Firstname }} // . and $firstname both refer to Firstname {{ end }}
|
|
|
|
Range:
|
|
{{ range .Users }} {{ .Firstname }} {{ end }}
|
|
|
|
{{ range $user := .Users }} {{ .Firstname }} {{ $user.Firstname }} {{ else }} No users found {{ end }}
|
|
|
|
{{ range $index, $user := .Users }} {{ end }}
|
|
|
|
Functions:
|
|
{{ if and cond1 cond2 … }}
|
|
|
|
{{ if or cond1 cond2 … }}
|
|
|
|
{{ if not cond }}
|
|
|
|
{{ len var }}
|
|
|
|
tmpl, _ := template.New(“template_name”).Parse(“...”)
|
|
|
|
tmpl.Name() => name of current template
|
|
|
|
tmpl.Execute(...) => execute current template
|
|
|
|
tmpl.ExecuteTemplate(...) => execute template by name
|
|
|
|
tmpl = tmpl.Lookup(“template_name”) => change current template
|
|
|
|
tmpl.Templates() => Get defined templates |