go-templates

Is there an efficient way to concatenate strings

夙愿已清 提交于 2021-02-05 20:12:55
问题 For example, there is a function like that: func TestFunc(str string) string { return strings.Trim(str," ") } It runs in the example below: {{ $var := printf "%s%s" "x" "y" }} {{ TestFunc $var }} Is there anyway to concatenate strings with operators in template ? {{ $var := "y" }} {{ TestFunc "x" + $var }} or {{ $var := "y" }} {{ TestFunc "x" + {$var} }} It gives unexpected "+" in operand error. I couldnt find it in documentation (https://golang.org/pkg/text/template/) 回答1: There is not a way

Is there an efficient way to concatenate strings

♀尐吖头ヾ 提交于 2021-02-05 20:10:37
问题 For example, there is a function like that: func TestFunc(str string) string { return strings.Trim(str," ") } It runs in the example below: {{ $var := printf "%s%s" "x" "y" }} {{ TestFunc $var }} Is there anyway to concatenate strings with operators in template ? {{ $var := "y" }} {{ TestFunc "x" + $var }} or {{ $var := "y" }} {{ TestFunc "x" + {$var} }} It gives unexpected "+" in operand error. I couldnt find it in documentation (https://golang.org/pkg/text/template/) 回答1: There is not a way

Placeholders are not replaced

断了今生、忘了曾经 提交于 2021-02-05 10:49:50
问题 I am experimenting with the package http/template . I have also already managed that e.g. the header, footer, navbar and so on were included in the base template: {{ define "base" }} <!DOCTYPE html> <html lang="en"> <!-- Start Head --> <head> {{ template "head" }} </head> <!-- End Head --> <!-- Start Body --> <body> {{ template "navbar" }} {{ template "content" }} {{ template "footer" }} </body> <!-- End Body --> </html> {{ end }} 404 page: {{ define "content" }} [...] <h1 class="text-light

Placeholders are not replaced

怎甘沉沦 提交于 2021-02-05 10:49:47
问题 I am experimenting with the package http/template . I have also already managed that e.g. the header, footer, navbar and so on were included in the base template: {{ define "base" }} <!DOCTYPE html> <html lang="en"> <!-- Start Head --> <head> {{ template "head" }} </head> <!-- End Head --> <!-- Start Body --> <body> {{ template "navbar" }} {{ template "content" }} {{ template "footer" }} </body> <!-- End Body --> </html> {{ end }} 404 page: {{ define "content" }} [...] <h1 class="text-light

How can I join two strings in go templates?

倖福魔咒の 提交于 2021-01-04 05:39:04
问题 I found this documentation to join two strings, but this doesn't work inside go templates. Is there a way to join strings inside a go template? 回答1: Write a function to join the strings and add it to the template func map: func join(s ...string) string { // first arg is sep, remaining args are strings to join return strings.Join(s[1:], s[0]) } Add the function to template before parsing: t, err := template.New(name).Funcs(template.FuncMap{"join": join}).Parse(text) Use it in the template like

How can I join two strings in go templates?

匆匆过客 提交于 2021-01-04 05:38:32
问题 I found this documentation to join two strings, but this doesn't work inside go templates. Is there a way to join strings inside a go template? 回答1: Write a function to join the strings and add it to the template func map: func join(s ...string) string { // first arg is sep, remaining args are strings to join return strings.Join(s[1:], s[0]) } Add the function to template before parsing: t, err := template.New(name).Funcs(template.FuncMap{"join": join}).Parse(text) Use it in the template like

How can I join two strings in go templates?

ぐ巨炮叔叔 提交于 2021-01-04 05:38:06
问题 I found this documentation to join two strings, but this doesn't work inside go templates. Is there a way to join strings inside a go template? 回答1: Write a function to join the strings and add it to the template func map: func join(s ...string) string { // first arg is sep, remaining args are strings to join return strings.Join(s[1:], s[0]) } Add the function to template before parsing: t, err := template.New(name).Funcs(template.FuncMap{"join": join}).Parse(text) Use it in the template like

How to index a slice element?

只谈情不闲聊 提交于 2020-12-28 04:29:50
问题 I have a slice: Keys []* datastore.Key How could I index one of them in the template file? I guessed {{.Keys[3] }} , but that doesn't work and I searched a lot but with no clue. Any suggestions would be welcome, thanks. 回答1: Use the index command like so: {{index .Keys 3}} 回答2: As stated in the html/template package, the majority of examples are actually located in the text/template pkg docs. See http://golang.org/pkg/text/template/ From the docs index Returns the result of indexing its first

How to index a slice element?

依然范特西╮ 提交于 2020-12-28 04:29:47
问题 I have a slice: Keys []* datastore.Key How could I index one of them in the template file? I guessed {{.Keys[3] }} , but that doesn't work and I searched a lot but with no clue. Any suggestions would be welcome, thanks. 回答1: Use the index command like so: {{index .Keys 3}} 回答2: As stated in the html/template package, the majority of examples are actually located in the text/template pkg docs. See http://golang.org/pkg/text/template/ From the docs index Returns the result of indexing its first

How to index a slice element?

与世无争的帅哥 提交于 2020-12-28 04:28:03
问题 I have a slice: Keys []* datastore.Key How could I index one of them in the template file? I guessed {{.Keys[3] }} , but that doesn't work and I searched a lot but with no clue. Any suggestions would be welcome, thanks. 回答1: Use the index command like so: {{index .Keys 3}} 回答2: As stated in the html/template package, the majority of examples are actually located in the text/template pkg docs. See http://golang.org/pkg/text/template/ From the docs index Returns the result of indexing its first