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.

Packages with this support include:

  • text/template
  • html/template

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)
537 questions
102
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
  • 7,216
  • 13
  • 48
  • 82
75
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
  • 23,178
  • 32
  • 115
  • 158
60
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

{{…
Acidic9
  • 8,255
  • 9
  • 46
  • 85
52
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
  • 12,741
  • 13
  • 52
  • 74
50
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,142
  • 4
  • 14
  • 16
48
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
  • 2,597
  • 2
  • 23
  • 30
47
votes
2 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
  • 13,286
  • 14
  • 63
  • 78
46
votes
3 answers

Switch or if/elseif/else inside golang HTML templates

I have this struct : const ( paragraph_hypothesis = 1<
Denys Séguret
  • 335,116
  • 73
  • 720
  • 697
46
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
  • 6,688
  • 2
  • 21
  • 26
34
votes
1 answer

In Go templates, accessing parent/global pipeline within range

Is it possible, within a {{range pipeline}} T1 {{end}} action in the text/template package to access the pipelines value prior to the range action, or the parent/global pipeline passed as an argument to Execute? Working example that shows what I try…
ANisus
  • 60,676
  • 28
  • 141
  • 147
34
votes
6 answers

Why am I seeing ZgotmplZ in my Go HTML template output?

When I'm calling a Go template function to output HTML, it displays ZgotmplZ. Sample code: http://play.golang.org/p/tfuJa_pFkm package main import ( "html/template" "os" ) func main() { funcMap := template.FuncMap{ …
Randy Proctor
  • 6,688
  • 2
  • 21
  • 26
33
votes
9 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
  • 5,559
  • 3
  • 23
  • 33
32
votes
8 answers

Go template.ExecuteTemplate include html

I have followed this tutorial: http://golang.org/doc/articles/wiki/final.go and have slightly modified it for my needs/wants. The problem is I would like to support HTML in the templates. I realize this is a security risk but it's not a concern at…
Peter
  • 2,682
  • 9
  • 28
  • 52
31
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,178
  • 1
  • 19
  • 31
31
votes
3 answers

Go template function

It noticed a weird thing with Go templates when I try to use Funcs and FuncMap. The following code works as expected: buffer := bytes.NewBufferString("") funcMap := template.FuncMap{ "label": strings.Title, } t, _ :=…
Blacksad
  • 13,286
  • 14
  • 63
  • 78
1
2 3
35 36