Go template function

ParseFiles could probably use better documentation. A template object can have multiple templates in it and each one has a name. If you look at the implementation of ParseFiles, you see that it uses the filename as the template name inside of the template object. So, name your file the same as the template object, … Read more

Go template: can’t evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)

The contents of dot (.) are assigned to $ after invocation of range, so you can use $ to access lang (on play): {{ range .users }} <form action=”/{{ $.lang }}/users” method=”POST”> <input type=”text” name=”Username” value=”{{ .Username }}”> <input type=”text” name=”Email” value=”{{ .Email }}”> </form> {{ end }} The behavior is documented here: When execution … Read more

why dash is used in condition Go templates

Dash removes the spaces from the output on the side it appears in the template: https://golang.org/pkg/text/template/#hdr-Text_and_spaces {{- if …}} The above will remove all spaces that appear before the if statement, so if the result of if prints something, it’ll be right after the last piece of text without any spaces.

Helm: generate comma separated list

For those from 2020+ it now can be achieved as simple as that: {{ join “,” .Values.some.array }} It produces the correct output: value: “test1,test2,test3″ At least it works with (more or less) recent versions of helm-cli: Client: &version.Version{SemVer:”v2.16.2″, GitCommit:”bbdfe5e7803a12bbdf97e94cd847859890cf4050″, GitTreeState:”clean”}

Why am I seeing ZgotmplZ in my Go HTML template output?

“ZgotmplZ” is a special value that indicates that unsafe content reached a CSS or URL context at runtime. The output of the example will be: <img src=”#ZgotmplZ”> You can add a safe and attr function to the template funcMap: package main import ( “html/template” “os” ) func main() { funcMap := template.FuncMap{ “attr”:func(s string) template.HTMLAttr{ … Read more

Go template name

The name of the template–unsurprisingly–is to name the template. What is it good for? As long as you don’t want to refer to the template, it doesn’t really matter. But if you want to refer to it, then yes, you refer to it by its name. When would you want to refer to it? When … Read more

Arithmetic in Go templates

You have to write a custom function to do this. http://play.golang.org/p/WsSakENaC3 package main import ( “os” “text/template” ) func main() { funcMap := template.FuncMap{ // The name “inc” is what the function will be called in the template text. “inc”: func(i int) int { return i + 1 }, } var strs []string strs = … Read more

Go template.ExecuteTemplate include html

Convert your []byte or string to type template.HTML (documented here) p.Body = template.HTML(s) // where s is a string or []byte Then, in your template, just: {{.Body}} It will be printed without escaping. EDIT In order to be able to include HTML in you page’s body you need to change the Page type declaration: type … Read more