Go templates range loop is quoting my value - go

I've got a slice of strings (.Table.PKey.Columns) that I'm trying to loop over in my template to generate a go file that does some appends, but when I output $value in my template, apparently Go is quoting it for me, so it is giving me the error:
5:27: expected selector or type assertion, found 'STRING' "ID"
i.e., instead of the template output looking something like o.ID -- which is what I'm aiming for, it ends up looking something like o."ID" (I presume).
Am I right in my assumption that this is the result of using a range loop? Because it seems when I access variables directly in other places (for example, say I had a string and I did: o.{{.Table.MyString}}) it works fine, but as soon as I try and incorporate a range loop into the mix it seems to be quoting things.
{{- range $key, $value := .Table.PKey.Columns }}
args = append(args, o.{{$value}})
{{ end -}}
Any suggestions? Thank you.

The {{range}} does not quote anything. If you see "ID" in your output, then your input value is "ID" with quotation marks included!
See this example:
func main() {
m := map[string]interface{}{
"Id": "Id1",
"Quoted": `"Id2"`,
"Ids": []string{"Id1", `"Id2"`, "Abc"},
}
t := template.Must(template.New("").Parse(src))
t.Execute(os.Stdout, m)
}
const src = `{{.Id}} {{index .Ids 0}} {{.Quoted}}
{{range $key, $value := .Ids}}{{$value}}
{{end}}
`
Output (try it on the Go Playground):
Id1 Id1 "Id2"
Id1
"Id2"
Abc

If the variable Go Template is rendering is within tags, then Go Template will wrap any strings with quotes for you.
Some options include generating the in code prior to asking Go Template to render it.

Related

Golang: Custom template "block" functions?

I'm wondering if it's possible to use a custom function as a template block with Golang templates. The code below shows an example.
{{ custom_func . }}
This is content that "custom_func" should do something with.
{{ end }}
Use case is a bit peculiar and non-standard. Basically I want the ability for the template author to pass in large block of text where newlines etc. is respected and for that entire block of text to be passed to the function. I could have done something like:
{{ custom_func "This is a lot of text\n with many lines etc." }}
But this is not very user friendly to the template author. The end goal is for them to write something like this:
Author is writing something normal...
{{ note }}
But would like to wrap this content as a "note".
Which when passed to the "note" function, will wrap the content with appropriate divs etc.
{{ end }}
Basically I'm trying an experiment to see if I can achieve "markdown/reStructuredText"-like content with pure go templates. It's mostly an experiment for now.
Eventually I'll probably need to write a proper PEG parser for this, but I want to see if this is possible first.
String arguments to functions may be wrapped both in double quotes " or in backticks `.
String literals wrapped in backticks in templates are called raw string constants, and they work like raw string literals in Go source: may include newlines (and cannot contain escape sequences).
So it's possible what you want if you use backticks for the argument.
For example, a.tmpl:
START
{{ note `a
b\t
c
d`}}
END
App to load and execute the template:
t := template.Must(template.New("").Funcs(template.FuncMap{
"note": func(s string) string { return "<note>\n" + s + "\n</note>" },
}).ParseFiles("a.tmpl"))
if err := t.ExecuteTemplate(os.Stdout, "a.tmpl", nil); err != nil {
panic(err)
}
This will output:
START
<note>
a
b\t
c
d
</note>
END
It's a bit tricky if you define the template in your Go source, as if you use backticks for the template text (because you want to write multiple lines), you can't embed backticks in a raw string literal. You have to break the literal, and concatenate the backticks.
Example doing this in a Go source file:
func main() {
t := template.Must(template.New("").Funcs(template.FuncMap{
"note": func(s string) string { return "<note>\n" + s + "\n</note>" },
}).Parse(src))
if err := t.Execute(os.Stdout, nil); err != nil {
panic(err)
}
}
const src = `START
{{ note ` + "`" + `a
b\t
c
d` + "`" + `}}
END
`
This will output the same, try it on the Go Playground.

Go Template compare if string ends in or contains another string

The eq function allows for comparing if two strings are equal
{{if eq .Name "MyName"}}
Is there a way to test if a string ends in (or contains) another string?
Use a function map containing the relevant string functions.
funcs := map[string]any{
"contains": strings.Contains,
"hasPrefix": strings.HasPrefix,
"hasSuffix": strings.HasSuffix}
tmpl := `{{if hasSuffix . ".txt"}}yes!{{end}}`
t := template.Must(template.New("").Funcs(funcs).Parse(tmpl))
t.Execute(os.Stdout, "example.txt") // writes yes! to standard out
Run the example on the playground.
Some applications that use Go templates as a feature (Hugo and Helm are examples) provide these functions by default.
(h/t to mkopriva).

How can I join strings in Go template without template functions?

I have a string slice, like x := []string{a,b,c}, and eventually I want it to be like a+"/"+b+"/"+c.
The problem I'm trying to deal with is coming from the Go template.
I want to have a solution in Go template.
The following is my current solution, but it's obviously ugly.
res := {{range item := .x}}item+"/"{{end}}
res = res[:len(res)-1]
If you have better approaches, appreciate it!
Use the following code to join a slice with /:
{{range $i, $v := .x}}{{if $i}}/{{end}}{{$v}}{{end}}
Run the example on the playground.

get the value of a go template from inside another template [duplicate]

This question already has an answer here:
Capture or assign golang template output to variable
(1 answer)
Closed 5 years ago.
I have two templates T1 and T2. I want to get the output of T1 and do a some extra processing on it inside T2. My question is:
how do I store the output of T1 in a variable inside T2? Is this even possible?
Here's some pseudo-template:
{{define "T1"}}
{{ printf "%s-%s" complex stuff }}
{{end}}
{{define "T2"}}
{{ $some_var := output_from_template "T1"}} <<<<<<<<<<<
{{ etc }}
{{end}}
There is no builtin support for storing the result of a template in a template variable, only for the inclusion of the result.
But you can register custom functions with any complex functionality you want. You may register a GetOutput function which would execute a template identified by its name, and it could return the result as a string, which you can store in a template variable.
Example doing this:
func main() {
t := template.New("")
t = template.Must(t.Funcs(template.FuncMap{
"GetOutput": func(name string) (string, error) {
buf := &bytes.Buffer{}
err := t.ExecuteTemplate(buf, name, nil)
return buf.String(), err
},
}).Parse(src))
if err := t.ExecuteTemplate(os.Stdout, "T2", nil); err != nil {
panic(err)
}
}
const src = `
{{define "T1"}}{{ printf "%s-%s" "complex" "stuff" }}{{end}}
{{define "T2"}}
{{ $t1Out := (GetOutput "T1")}}
{{ printf "%s-%s" "even-more" $t1Out }}
{{end}}`
Output will be (try it on the Go Playground):
even-more-complex-stuff
The "T1" template simply outputs "complex-stuff", and the "T2" template gets the output of "T1", and concatenates the static text "even-more-" and the result of "T1".
The registered GetOutput function gets the name of a template to execute, executes it by directing its output to a local buffer, and returns the content of the buffer (along with the optional error of its execution).
Edit: I've found an exact duplicate: Capture or assign golang template output to variable

Iterate through map in Go text template

I have a map of values that looks like this:
vals := map[string]interface{}{"foo": 1, "bar": 2, "baz": 7}
data := map[string]interface{}{"bat": "obj", "values": vals}
What should my template look like to generate the following string (note the correct comma usage)?
SET obj.foo=1, obj.bar=2, obj.baz=7
I started with this as my template:
SET {{range $i, $v := .values}} {{.bat}}.{{$i}}={{$v}},{{end}}
But that just prints out
SET
And even if that did work, the commas would be incorrect. I then tried to use a custom function to format the map, but I couldn't get the template to ever call my function. None of the following seemed to work:
SET {{.MyFunction .values}}
SET {{call .MyFunction .values}}
SET {{call MyFunction .values}}
when MyFunction was defined as:
func MyFunction(data map[string]interface{}) string {
fmt.PrintLn('i was called!')
return "foo"
}
And I'm executing the templates using a helper function that looks like this:
func useTemplate(name string, data interface{}) string {
out := new(bytes.Buffer)
templates[name].Execute(out, data)
return string(out.Bytes())
}
Thanks!
This will get you pretty close:
SET {{range $key, $value := $.values}}{{$.bat}}.{{$key}}={{$value}} {{end}}
rendering as:
SET obj.bar=2 obj.baz=7 obj.foo=1
Unfortunately, I don't think there's any simple way to have the commas added in between the values due to how the range action iterates on maps (there's no numeric index). That said, the template packages were meant to be easily extensible so you can have less logic in your templates and more logic in Go itself, so it's easy enough to code a helper function in Go and make it available to your templates.
If you're happy to go that extra mile, then the template becomes much simpler, and also more efficient. The function can look like this:
func commaJoin(prefix string, m map[string]interface{}) string {
var buf bytes.Buffer
first := true
for k, v := range m {
if !first {
buf.WriteString(", ")
}
first = false
buf.WriteString(prefix)
buf.WriteByte('.')
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(fmt.Sprint(v))
}
return buf.String()
}
and your template would look like:
SET {{$.values | commaJoin $.bat}}
Here is a working example with this logic:
http://play.golang.org/p/5lFUpFCzZm

Resources