go-templates

Idiomatic way to handle template errors in golang

荒凉一梦 提交于 2019-12-05 03:53:49
Say I have a html/template like the following: <html> <body> <p>{{SomeFunc .SomeData}}</p> </body> and sometimes SomeFunc returns an error. Is there an idiomatic way to deal with this? If I write directly to the ResponseWriter , then a status code 200 has already been written before I encounter the error. var tmpl *template.Template func Handler(w http.ResponseWriter, r *http.Request) { err := tmpl.Execute(w, data) // "<html><body><p>" has already been written... // what to do with err? } Preferably I would return a status code 400 or some such, but I can't see a way to do this if I use

Kubernetes Helm, combine two variables with a string in the middle

拜拜、爱过 提交于 2019-12-04 22:57:36
I’m trying to change the value of a variable if another variable it set by combining the two with a dash in the middle, I’m not sure of the syntax to do this, I’m thinking of somethings like: {{- $serviceNamespace := .Values.serviceNamespace -}} {{- $serviceTag := .Values.serviceTag -}} {{- if $serviceTag}} {{- $serviceNamespace := .Values.serviceNamespace "-" .Values.serviceTag -}} {{- end}} Is this correct? if serviceNamespace was hello and serviceTag was 1.0.0 would I end up with serviceNamespace being hello-1.0.0 ? For concatenation just use printf: {{- $serviceNamespace := printf "%s-%s"

Last item in a template range

微笑、不失礼 提交于 2019-12-04 19:16:00
问题 Given the template: {{range $i, $e := .SomeField}} {{if $i}}, {{end}} $e.TheString {{end}} This can output: one, two, three If, however, I want to output: one, two, and three I'd need to know which is the last element in the range above. I can set a variable that holds the length of the array .SomeField , but that will always be 3, and the $i value above will only ever get to 2. And you can't perform arithmetic in templates from what I've seen. Is detecting the last value in a template range

Helm: generate comma separated list

若如初见. 提交于 2019-12-04 12:01:44
问题 Using Helm templates, I'm trying to generate a list of server names based on a number in values.yaml. The dot for this template is set to the number (its a float64). {{- define "zkservers" -}} {{- $zkservers := list -}} {{- range int . | until -}} {{- $zkservers := print "zk-" . ".zookeeper" | append $zkservers -}} {{- end -}} {{- join "," $zkservers -}} {{- end -}} For an input of, say, 3 I'm expecting this to produce: zk-0.zookeeper,zk-1.zookeeper,zk-2.zookeeper It produces nothing. I

Variable value as yaml key in helm chart

六月ゝ 毕业季﹏ 提交于 2019-12-04 01:51:17
问题 I want to choose config section from values.yaml by setting a variable in helm command line. example part of values.yaml: aaa: x1: "az1" x2: "az2" bbb: x1: "bz1" x2: "bz2" example part of configmap.yaml data: {{ .Values.outsideVal.x1 }} Expected result should looks like this data: az1 Test helm output helm template --set outsideVal=aaa mychart And got this error Error: render error in "./templates/configmap.yaml": template: ./templates/configmap.yaml:21:12: executing "./templates/configmap

Golang template variable isset

╄→尐↘猪︶ㄣ 提交于 2019-12-04 01:07:18
问题 I have created a function to check if a variable is defined: fm["isset"] = func(a interface{}) bool { if a == nil || a == "" || a == 0 { fmt.Println("is not set") return false } fmt.Println("is set") return false } tmpl := template.Must(template.New("").Funcs(fm).ParseFiles("templates/header.html")) err := tmpl.ExecuteTemplate(w, "header", templateData) In the template I have: {{ if isset .Email }} email is set {{ end }} This function works if the variable is contained by the templateData

Referencing Go array in Javascript

喜夏-厌秋 提交于 2019-12-03 17:53:35
问题 I have a Golang array I'm passing to my html file on the front end. I know that '{{ index .Array 0}}' works and pulls the first element from the array. But I want to do a Javascript for-loop and print every element in the array like so <script type="text/javascript"> function loop() { html = "" for(var i = 0; i<5; i++) { html += "{{ index .Array " + i + "}}"; } } But this doesn't work. Something about separating the go array index string, HTML/Javascript doesn't like it and it won't load. It

How to range over slice of structs instead of struct of slices

依然范特西╮ 提交于 2019-12-03 17:33:25
问题 Having played around with Go HTML templates a bit, all the examples I found for looping over objects in templates were passing structs of slices to the template, somewhat like in this example : type UserList struct { Id []int Name []string } var templates = template.Must(template.ParseFiles("main.html")) func rootHandler(w http.ResponseWriter, r *http.Request) { users := UserList{ Id: []int{0, 1, 2, 3, 4, 5, 6, 7}, Name: []string{"user0", "user1", "user2", "user3", "user4"}, } templates

Helm _helpers.tpl: Calling defined templates in other template definitions

我是研究僧i 提交于 2019-12-03 16:29:09
问题 Helm _helpers.tpl? Helm allows for the use of Go templating in resource files for Kubernetes. A file named _helpers.tpl is usually used to define Go template helpers with this syntax: {{- define "yourFnName" -}} {{- printf "%s-%s" .Values.name .Values.version | trunc 63 -}} {{- end -}} Which you can then use in your *.yaml resource files like so: {{ template "yourFnName" . }} The Question How can I use the helpers I define, in other helper definitions? For example, what if I have a helper for

Helm: generate comma separated list

耗尽温柔 提交于 2019-12-03 13:31:59
Using Helm templates, I'm trying to generate a list of server names based on a number in values.yaml. The dot for this template is set to the number (its a float64). {{- define "zkservers" -}} {{- $zkservers := list -}} {{- range int . | until -}} {{- $zkservers := print "zk-" . ".zookeeper" | append $zkservers -}} {{- end -}} {{- join "," $zkservers -}} {{- end -}} For an input of, say, 3 I'm expecting this to produce: zk-0.zookeeper,zk-1.zookeeper,zk-2.zookeeper It produces nothing. I understand that the line within the range block is a no-op since the variable $zkservers is a new variable