go-templates

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

梦想的初衷 提交于 2019-12-10 01:23:51
问题 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

Join range block in go template

五迷三道 提交于 2019-12-08 07:14:54
问题 I have a go template like this: "environment": [ {{- range $k,$v := .env }} { "name": "{{ $k }}", "value": "{{ $v }}" }, {{- end }} ] and i am getting the output below: "environment": [ { "name": "name", "value": "test" }, { "name": "region", "value": "us-east-1" }, ] and i want to render it like below: "environment": [ { "name": "name", "value": "bxbd" }, { "name": "region", "value": "us-east-1" } ] I am not able to get rid of the last comma to make valid json. Or is it possible to somehow

ParseGlob: What is the pattern to parse all templates recursively within a directory?

天大地大妈咪最大 提交于 2019-12-07 13:48:24
问题 Template.ParseGlob("*.html") //fetches all html files from current directory. Template.ParseGlob("**/*.html") //Seems to only fetch at one level depth Im not looking for a "Walk" solution. Just want to know if this is possible. I don't quite understand what "pattern" this expects. if i can get an explanation about the pattern used by ParseGlob that would be great too. 回答1: The code text/template/helper.go mentions // The pattern is processed by filepath.Glob and must match at least one file.

Go lang templates: always quotes a string and removes comments

て烟熏妆下的殇ゞ 提交于 2019-12-07 13:00:54
问题 This Go code always quotes a string: http://play.golang.org/p/8k4s8dv2PE in the template - you can see results. How can I generate var currentUser = null ? Note it also removes all comments from the code! How is it tuned up? This question is continuation of my Go: quoted string in templates. 回答1: The html/template package is expressly designed to escape values. In your case, you're trying to pass JavaScript code in, rather than a simple value. You can accomplish this by changing the type of

Idiomatic way to handle template errors in golang

时间秒杀一切 提交于 2019-12-06 23:04:40
问题 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? }

Join range block in go template

那年仲夏 提交于 2019-12-06 15:32:34
I have a go template like this: "environment": [ {{- range $k,$v := .env }} { "name": "{{ $k }}", "value": "{{ $v }}" }, {{- end }} ] and i am getting the output below: "environment": [ { "name": "name", "value": "test" }, { "name": "region", "value": "us-east-1" }, ] and i want to render it like below: "environment": [ { "name": "name", "value": "bxbd" }, { "name": "region", "value": "us-east-1" } ] I am not able to get rid of the last comma to make valid json. Or is it possible to somehow send the complete range block to some custom join function? Here's an example how to do it with

How to write template output to a file in Golang?

ⅰ亾dé卋堺 提交于 2019-12-06 09:36:37
I use the following code which work ok, but now I want to print the template to a file and tried the following but got error package main import ( "html/template" "log" "os" ) func main() { t := template.Must(template.New("").Parse(`{{- range .}}{{.}}: echo "from {{.}}" {{end}} `)) t.Execute(os.Stdout, []string{"app1", "app2", "app3"}) f, err := os.Create("./myfile") if err != nil { log.Println("create file: ", err) return } err = t.Execute(f, t) if err != nil { log.Print("execute: ", err) return } f.Close() } The error is: execute: template: :1:10: executing "" at <.>: range can't iterate

Go lang templates: always quotes a string and removes comments

女生的网名这么多〃 提交于 2019-12-05 23:45:49
This Go code always quotes a string: http://play.golang.org/p/8k4s8dv2PE in the template - you can see results. How can I generate var currentUser = null ? Note it also removes all comments from the code! How is it tuned up? This question is continuation of my Go: quoted string in templates . The html/template package is expressly designed to escape values. In your case, you're trying to pass JavaScript code in, rather than a simple value. You can accomplish this by changing the type of UserEmail to that of template.JS . This type wraps a string and expresses the intent that this value is

ParseGlob: What is the pattern to parse all templates recursively within a directory?

不想你离开。 提交于 2019-12-05 21:46:10
Template.ParseGlob("*.html") //fetches all html files from current directory. Template.ParseGlob("**/*.html") //Seems to only fetch at one level depth Im not looking for a "Walk" solution. Just want to know if this is possible. I don't quite understand what "pattern" this expects. if i can get an explanation about the pattern used by ParseGlob that would be great too. The code text/template/helper.go mentions // The pattern is processed by filepath.Glob and must match at least one file. filepath.Glob() says that "the syntax of patterns is the same as in Match " Match returns true if name

Show default content in a template if an object is nil otherwise show based on the set property

馋奶兔 提交于 2019-12-05 10:31:38
In my template, I would like to include some default meta tags (90% of the time). However, when a specific property is set, I would like to show a different set of text. I know I can set an anonymous struct and set a property with either "default" or "some-x" . However, this means, I need to add an anonymous struct to 90% of my handlers that just currently pass nil . Is there way to do something like {{if eq . nil}} // default meta tag {{else if eq .MetaValue "some-x"}} //other {{end}} If I try something like my above code, it compiles but doesn't do what I want. Appreciate any suggestions on