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
43
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
  • 74,460
  • 29
  • 162
  • 158
42
votes
4 answers

Arithmetic in Go templates

I am trying to achieve a very simple thing in a Go template and failing! The range action allows me to iterate through an array along with its zero-based index, as so: {{range $index, $element := .Pages}} Number: {{$index}}, Text:…
Ripul
  • 1,271
  • 4
  • 15
  • 18
42
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
  • 3,144
  • 11
  • 37
  • 56
41
votes
3 answers

Go template name

In the html/template (and text/template) packages, template.New has the following signature: func New(name string) *Template What exactly is the name used for? I've scanned the docs (and a bit of source), but to no avail. I just instantiate all of…
Dave
  • 4,356
  • 4
  • 37
  • 40
40
votes
7 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
  • 7,396
  • 3
  • 25
  • 26
34
votes
8 answers

Helm: generate comma separated list

Using Helm templates, I'm trying to generate a list of server names based on a number in values.yaml. The dot for this template is set to the number (its a float64). {{- define "zkservers" -}} {{- $zkservers := list -}} {{- range int . | until…
alfred
  • 639
  • 1
  • 6
  • 11
34
votes
2 answers

Helm _helpers.tpl: Calling defined templates in other template definitions

Helm _helpers.tpl? Helm allows for the use of Go templating in resource files for Kubernetes. A file named _helpers.tpl is usually used to define Go template helpers with this syntax: {{- define "yourFnName" -}} {{- printf "%s-%s" .Values.name…
Noah Huppert
  • 4,028
  • 6
  • 36
  • 58
31
votes
1 answer

why dash is used in condition Go templates

I've seen many examples of dash being use for if statements ("{{- if.."), e.g: {{- if hasKey .Values.mymap "mykey" }} # do something conditional here... {{- end }} what is the purpose of the dash in that statement?
amit
  • 2,171
  • 4
  • 31
  • 50
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
  • 14,906
  • 15
  • 70
  • 81
30
votes
2 answers

Go template: can't evaluate field X in type Y (X not part of Y but stuck in a {{range}} loop)

Similar question answered here, but I don't think it solves my problem. Let's say you have the following struct: type User struct { Username string Password []byte Email string ... } Moreover, the URL hasa structure like this:…
fisker
  • 979
  • 4
  • 18
  • 28
28
votes
5 answers

How can you call a helm 'helper' template from a subchart with the correct context?

Helm charts define helper templates in _helpers.tpl which are used to create normalized names for the services. The standard form of the template for a service (DNS) name is: {{- define "postgresql.fullname" -}} {{- $name := default .Chart.Name…
moreginger
  • 530
  • 1
  • 5
  • 10
27
votes
3 answers

How can I conditionally set a variable in a Go template based on an expression which may cause an error if not wrapped with an if statement

Question How do I do something like this: {{ $use_ssl := (ne $.Env.CERT_NAME "") }} where $.Env.CERT_NAME may be nil/undefined. If it is nil, it gives this error: at : error calling ne: invalid type for comparison Note: I…
Tyler Rick
  • 9,191
  • 6
  • 60
  • 60
26
votes
8 answers

How to pass dynamic arguments to a helm chart that runs a job

I'd like to allow our developers to pass dynamic arguments to a helm template (Kubernetes job). Currently my arguments in the helm template are somewhat static (apart from certain values) and look like this Args: --arg1 …
Aaron Bandelli
  • 1,238
  • 2
  • 14
  • 16
25
votes
3 answers

Variables inside templates in golang

What is the namespace of variables inside html/text templates? I thought that a variable $x can change value inside a template, but this example shows me that I cannot. I failed when I tried to group tournaments according year - something like this…
lofcek
  • 1,165
  • 2
  • 9
  • 18
24
votes
3 answers

Implement a for loop inside a Go template

I am working in Go, and right now I need to print at least 20 options inside a select, so I need to use some kind of loop that goes from 0 to 20 (to get an index). How can I use a for loop inside a Go template? I need to generate the sequence of…
Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115
1
2
3
51 52