Questions tagged [go-templates]

Go language supports built-in template functionality. Packages with this support include 1) text/template and; 2) html/template.

Overview

Go language supports built-in template functionality. This is provided by the text/template and html/template packages. Go templates can be used as a minimal scripting language within command-line tools, or embedded in tools to produce larger files.

Features

Go language templates support most of the traditional features found in template-engines including, but not limited to:

  • comments
  • template variables and expressions
  • {{ }} template placeholder syntax (known as actions)

Related Tools

Go templates are widely used in the greater ecosystem. A template can be used to render output of the command-line tool, or in combination with the package manager to produce Kubernetes object manifests based on deploy-time settings.

Tag Guidance

Go templates are not Go code. Use the tag only if you are writing code that invokes one of the template packages; do not use it if you are writing template code using another tool using the template engine.

If you are using Go templates in the context of another tool such as or , also use the appropriate tag for that tool. If you are using a support library such as , it may be appropriate to include this tag as well.

Go templates share many aspects of traditional programming, including variables, loops, and subroutines. It is possible to use Go templates in non-programming contexts as well. For simple applications merely involving variable substitution or rendering a structure in the --format option of a command-line tool, consider whether your use is actually programming-related.

779 questions
23
votes
2 answers

What is, and what use cases have the dot "." in helm charts?

im currently going through the docs of helm, and there have been at least 3 different uses for the dot (.), is there any specific definition for it? and does it have something in common with the bash use (actual folder/files)? some cases in the…
Juan.
  • 652
  • 2
  • 9
  • 20
22
votes
2 answers

Range over string slice in golang template

I have a struct that contains a slice of type string like the following. type Data struct { DataFields []string } Within my html template file I would like to range over the string slice. However, the individual fields are just strings…
JetThomas
  • 373
  • 1
  • 3
  • 10
22
votes
2 answers

Go template comparison operators on missing map key

I am unable to find any documentation regarding what the type of the return value is when attempting key into a map in which the key doesn't exist. From the Go bug tracker it appears to be a special 'no value' I'm trying to compare two values using…
Sam
  • 1,564
  • 4
  • 23
  • 37
21
votes
1 answer

Using include inside range in Go templates (helm)

I have a template that get rendered several times with a range iteration and I can access variables external variables such as $.Release.Name without a problem. However, when I include templates I can't get it to work: {{ range $key, $val :=…
znat
  • 13,144
  • 17
  • 71
  • 106
21
votes
2 answers

Multiple files using template.ParseFiles in golang

For example.go, I have package main import "html/template" import "net/http" func handler(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("header.html", "footer.html") t.Execute(w, map[string] string {"Title": "My…
Tech163
  • 4,176
  • 8
  • 33
  • 36
20
votes
1 answer

How to access value of first index of array in Go templates

So I have and html template when using this I get the object:
Foobar {{ index .Doc.Users 0}}
Output:
Foobar {MyName my@email.com}
I just want to use the Name field I have tried many iterations without success: {{ index…
CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
20
votes
3 answers

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

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"…
Ecognium
  • 2,046
  • 1
  • 19
  • 35
20
votes
4 answers

Last item in a template range

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…
mmm111mmm
  • 3,607
  • 4
  • 27
  • 44
20
votes
2 answers

How to print JSON on golang template?

I need a object in client side, so I converted it to JSON using json.marshal and printed it into template. The object is getting printed as escaped JSON string. I'm expecting it to be var arr=["o1","o2"] but it is var arr="[\"o1\",\"o2\"]" I know I…
Chakradar Raju
  • 2,691
  • 2
  • 26
  • 42
19
votes
1 answer

Is there an efficient way to concatenate strings

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…
Özgür Yalçın
  • 1,872
  • 3
  • 16
  • 26
19
votes
6 answers

Call other templates with dynamic name

I do not see a way to call templates (text or html) with a dynamic name. Example: This works: {{template "Blah" .}} This errors with "unexpected "$BlahVar" in template invocation": {{$BlahVar := "Blah"}} {{template $BlahVar .}} The overall…
Brad Peabody
  • 10,917
  • 9
  • 44
  • 63
17
votes
3 answers

Error "property value expected css" and "at-rule or selector expected css" editing Go template in VSCode

I'm using Go and package html/template. This is my code in mypage.tmpl:
But I get errors in VSCode: property value expected…
Nam Lee
  • 891
  • 1
  • 9
  • 20
16
votes
3 answers

Go template remove the last comma in range loop

I have code like this: package main import ( "text/template" "os" ) func main() { type Map map[string]string m := Map { "a": "b", "c": "d", } const temp = `{{range $key, $value := $}}key:{{$key}}…
Chris
  • 191
  • 2
  • 9
16
votes
1 answer

Dereferencing pointers in a golang text/template

Inside a golang template when simply outputting values it seems that pointers are automatically dereferenced. When .ID is a pointer to an int, {{.ID}} outputs 5 But when I try to use it in a pipeline, {{if eq .ID 5}} I get an error. executing…
Aruna Herath
  • 6,241
  • 1
  • 40
  • 59
15
votes
1 answer

Helm template: get node of first array element

Say I have these values grafana: ... ingress: enabled: true annotations: {} # kubernetes.io/ingress.class: nginx # kubernetes.io/tls-acme: "true" hosts: - host: chart-example.local paths: ["/grafana"] This…
zar3bski
  • 2,773
  • 7
  • 25
  • 58
1 2
3
51 52