In Go templates, accessing parent/global pipeline within range

Using the $ variable (recommended) From the package text/template documentation: When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot. As @Sandy points out, it is therefore possible to access the Path in the outer scope using $.Path. const page = `{{range .Files}}<script src=”https://stackoverflow.com/questions/17284222/{{html … Read more

Compare strings in templates

eq is function, not an operator. It is called with the form: eq <x> <y> (not <x> eq <y>). You can fix your template by moving the operands from the the sides of eq to after it: {{if eq . “login failed”}} <span class=”text-error”>Incorrect username or password</span> {{else if eq . “login success”}} <span class=”text-success”>You … Read more

Calling a template with several pipeline parameters

You could register a “dict” function in your templates that you can use to pass multiple values to a template call. The call itself would then look like that: {{template “userlist” dict “Users” .MostPopular “Current” .CurrentUser}} The code for the little “dict” helper, including registering it as a template func is here: var tmpl = … Read more

Call a method from a Go template

Just omit the parentheses and it should be fine. Example: package main import ( “html/template” “log” “os” ) type Person string func (p Person) Label() string { return “This is ” + string(p) } func main() { tmpl, err := template.New(“”).Parse(`{{.Label}}`) if err != nil { log.Fatalf(“Parse: %v”, err) } tmpl.Execute(os.Stdout, Person(“Bob”)) } According to … Read more

How to make nested variables optional in Helm

Simple workaround Wrap each nullable level with parentheses (). {{ ((.Values.foo).bar) }} Or {{ if ((.Values.foo).bar) }} {{ .Values.foo.bar }} {{ end }} How does it work? Helm uses the go text/template and inherits the behaviours from there. Each pair of parentheses () can be considered a pipeline. From the doc (https://pkg.go.dev/text/template#hdr-Actions) It is: The … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)