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
129
votes
2 answers

Iterating through map in template

I am trying to display a list gym classes (Yoga, Pilates etc). For each class type there are several classes, so I want to group all the Yoga classes, and all the Pilates classes and so on. I made this function to take a slice and make a map of…
Lee
  • 8,354
  • 14
  • 55
  • 90
108
votes
2 answers

Access a map value using a variable key in a Go template

How can I look up the value of a map by using a variable key without iterating? So one can lookup a constant key on variable map $x with $x.key1, but is it possible to do amap.$key?
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
99
votes
5 answers

Is it possible to have nested templates in Go using the standard library?

How do I get nested templates like Jinja has in the python runtime. TBC what I mean is how do I have a bunch of templates inherit from a base templates, just filing in blocks of the base templates, like Jinja/django-templates does. Is it possible…
Sri Kadimisetty
  • 1,804
  • 4
  • 23
  • 25
83
votes
3 answers

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

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 -}} {{-…
Simon I
  • 3,406
  • 4
  • 26
  • 32
76
votes
5 answers

What kubectl command can I use to get events sorted by specific fields and print only specific details of events?

I need to print only specific fields of Kubernetes Events, sorted by a specific field. This is to help me gather telemetry and analytics about my namespace How could I do that?
suryakrupa
  • 3,852
  • 1
  • 25
  • 34
75
votes
1 answer

If not true (!true)

In golang's template/html package, I can use {{ if .loggedIn }} to check if logged in is true. How do I check if .loggedIn is false without using ne or eq? For example, I am looking for something like {{ if !.loggedIn }}

Not logged in

{{…
Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
68
votes
3 answers

Switch or if/elseif/else inside golang HTML templates

I have this struct : const ( paragraph_hypothesis = 1<
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
65
votes
6 answers

How to make nested variables optional in Helm

How do I make an optional block in the values file and then refer to it in the template? For examples, say I have a values file that looks like the following: # values.yaml foo: bar: "something" And then I have a helm template that looks like…
Joe J
  • 9,985
  • 16
  • 68
  • 100
62
votes
4 answers

How do I escape “{{” and “}}” delimiters in Go templates?

I’m using AngularJS as the front-end JS library, with Go templates within Revel framework to to generate the markup on the back-end. But both Go and Angular use {{ and }} for delimiters in their templates. How can I escape them in Go to pass them to…
Coder1
  • 13,139
  • 15
  • 59
  • 89
62
votes
2 answers

In a template how do you access an outer scope while inside of a "with" or "range" scope?

When inside a with or range, the scope of . is changed. How do you access the calling scope?
Randy Proctor
  • 7,396
  • 3
  • 25
  • 26
58
votes
2 answers

multiple conditions in if statement Go templates

how can I have multiple conditions in an if statement inside a template? I tried this code: {{ if .condition1 && .condition2 }} {{ end }} But it doesn't work. (in fact it panics)
Berry Jones
  • 857
  • 2
  • 7
  • 11
58
votes
3 answers

Call a method from a Go template

Let's say I have type Person struct { Name string } func (p *Person) Label() string { return "This is " + p.Name } How can I use this method from a html/template ? I would need something like this in my template: {{ .Label() }}
Blacksad
  • 14,906
  • 15
  • 70
  • 81
57
votes
2 answers

How to index a slice element?

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.
DeanSinaean
  • 2,297
  • 4
  • 16
  • 19
47
votes
11 answers

Calling a template with several pipeline parameters

In a Go template, sometimes the way to pass the right data to the right template feels awkward to me. Calling a template with a pipeline parameter looks like calling a function with only one parameter. Let's say I have a site for Gophers about…
Deleplace
  • 6,812
  • 5
  • 28
  • 41
46
votes
2 answers

Compare strings in templates

I have the following template: {{if . eq "login failed"}} Incorrect username or password {{else if . eq "login success"}} You have successfully logged in! {{end}} I am passing a…
callmekatootie
  • 10,989
  • 15
  • 69
  • 104
1
2 3
51 52