{{/* comment */}}
printf
# php print_r equivalent
{{printf "%+v" .Raw}}
{{$href := ""}}
{{if .Tas.Actif}}
{{$href = printf "/stockage/liste#tas-%d" .Tas.Id}}
{{else}}
{{$href = printf "/tas-vides#tas-%d" .Tas.Id}}
{{end}}
"{{- " : trim trailing whitespace of preceeding text
" -}}" : trim heading whitespace of following text
Variables
$ = value of the variable passed to the template (even inside a loop)
ex : $.Details
$.Id = 12
{{$idStockage := .Id}}
Arrays
{{len .Chantier.Tas}}
{{(index .Chantier.Tas 0).Id}} # field Id of element index 0 of .Chantier.Tas
"index x 1 2 3" # x[1][2][3]
Simple range, without index (. = current element)
{{range .Frais}}
| {{.TypeFrais | labelStockFrais}} |
{{end}}
Range with index :
range $index, $element := pipeline
{{range $i, $ug := .UGs}}
console.log({{$i}});
console.log({{$ug}});
{{end}}
# .Periods = regular array ; each elt is a regular array containing 2 dates
{{range $val := .Periods}}
# .Periods[i][0] - .Periods[i][1]
{{(index $val 0) | dateFr}} - {{(index $val 1) | dateFr}}
{{end}}
Maps
Range on a k, v map :
{{range $key, $value := .}}
{{$key}}: {{$value}}
{{end}}
{{index .Amap "key1"}} # = Amap["key1"]
# test if a key exists in a map
# The index function returns the zero value for the map value type when the key is not in the map.
{{if eq (index .MyMap "KeyThatDoesntExist") "mystring"}}
{{.}}
{{end}}
Pipelines
{{pipeline}}
{{if pipeline}} T1 {{end}}
{{if pipeline}} T1 {{else}} T0 {{end}}
{{if pipeline}} T1 {{else if pipeline}} T0 {{end}}
the same as writing
{{if pipeline}} T1 {{else}}{{if pipeline}} T0 {{end}}{{end}}
{{range pipeline}} T1 {{end}}
{{range pipeline}} T1 {{else}} T0 {{end}}
{{template "name"}}
{{template "name" pipeline}}
{{block "name" pipeline}} T1 {{end}}
A block is shorthand for defining a template
{{define "name"}} T1 {{end}}
and then executing it in place
{{template "name" .}}
{{with pipeline}} T1 {{end}}
{{with pipeline}} T1 {{else}} T0 {{end}}
Functions
if(var1 == "" && var2 == "") {{if (and (eq var1 "") (eq var2 ""))}}
if(!(.Fournisseur || .Proprietaire) {{if(not (or .Fournisseur .Proprietaire))}}
if(.TypeCout == "C") {{if eq .TypeCout "C"}}
x[1][2][3] index x 1 2 3
if(!.IsSctl) {{if not .IsSctl}}
{{if not .DatePaiement.IsZero}} Fields and methods of variables can be used
{{printf "%.2f" .Price}}
and
Returns the boolean AND of its arguments by returning the
first empty argument or the last argument, that is,
"and x y" behaves as "if x then y else x". All the
arguments are evaluated.
call
Returns the result of calling the first argument, which
must be a function, with the remaining arguments as parameters.
Thus "call .X.Y 1 2" is, in Go notation, dot.X.Y(1, 2) where
Y is a func-valued field, map entry, or the like.
The first argument must be the result of an evaluation
that yields a value of function type (as distinct from
a predefined function such as print). The function must
return either one or two result values, the second of which
is of type error. If the arguments don't match the function
or the returned error value is non-nil, execution stops.
html
Returns the escaped HTML equivalent of the textual
representation of its arguments. This function is unavailable
in html/template, with a few exceptions.
index
Returns the result of indexing its first argument by the
following arguments. Thus "index x 1 2 3" is, in Go syntax,
x[1][2][3]. Each indexed item must be a map, slice, or array.
slice
slice returns the result of slicing its first argument by the
remaining arguments. Thus "slice x 1 2" is, in Go syntax, x[1:2],
while "slice x" is x[:], "slice x 1" is x[1:], and "slice x 1 2 3"
is x[1:2:3]. The first argument must be a string, slice, or array.
js
Returns the escaped JavaScript equivalent of the textual
representation of its arguments.
len
Returns the integer length of its argument.
not
Returns the boolean negation of its single argument.
or
Returns the boolean OR of its arguments by returning the
first non-empty argument or the last argument, that is,
"or x y" behaves as "if x then x else y". All the
arguments are evaluated.
print
An alias for fmt.Sprint
printf
An alias for fmt.Sprintf
println
An alias for fmt.Sprintln
urlquery
Returns the escaped value of the textual representation of
its arguments in a form suitable for embedding in a URL query.
This function is unavailable in html/template, with a few
exceptions.
Equality operators
eq
Returns the boolean truth of arg1 == arg2
ne
Returns the boolean truth of arg1 != arg2
lt
Returns the boolean truth of arg1 < arg2
le
Returns the boolean truth of arg1 <= arg2
gt
Returns the boolean truth of arg1 > arg2
ge
Returns the boolean truth of arg1 >= arg2
eq (only) accepts two or more arguments and compares the second and subsequent to the first, returning in effect
arg1==arg2 || arg1==arg3 || arg1==arg4 ...
# test several conditions
{{if or .Cp .Ville}} ... {{end}}