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

Assign Golang variable to Javascript

Currently I am having an issue related to assign a Golang variable to a Javascript variable. I am using the Golang templates, so, from the backend I sent a JSON variable, just like this: var c []models.C b, _ := json.Marshal(c) err =…
Sredny M Casanova
  • 4,735
  • 21
  • 70
  • 115
7
votes
2 answers

In a Go template range loop, are variables declared outside the loop reset on each iteration?

I'm trying to use a variable declared outside a Go template range loop to see if the previous post occurred on the same day as the current post. Here's a simplified example. Where .Posts is an array of post structs that each have a .Content and a…
Del Putnam
  • 273
  • 4
  • 8
6
votes
1 answer

What causes Helm chart template to throw 'unexpected EOF'?

I'm trying to add ingress to my nginx container. The following ingress template gives me "parse error (<>/ingress.yaml:71: unexpected EOF)". I went through trying mark possible missing end statements, but even adding arbitrary end at the end of file…
Zerg Overmind
  • 955
  • 2
  • 14
  • 28
6
votes
1 answer

helm chart: Include multiple lines from values.yaml into configmap

I want to create a helm chart that results in a config map that looks like this: apiVersion: v1 kind: ConfigMap metadata: name: myconfigmap data: myconfigfile1.properties: | property11 = value11 property12 = value12 …
Martin Kirchner
  • 171
  • 1
  • 1
  • 7
6
votes
3 answers

Creating a filtered list using helm template helpers

I'm trying to use a helm template helper to filter out values from a list in my values.yaml file, based on the value of one of the keys in each list member. My chart is currently comprised of these files - values.yaml - namespaces: - name: filter …
6
votes
1 answer

Size list with helm

Simple question is it possible to get size list with helm and sprig function ? My list : list: - a - b - c I tried like this : {{ .Values.list | len }} {{ .Values.list | size }} {{ .Values.list | length }}
M.Hol
  • 365
  • 2
  • 4
  • 15
6
votes
0 answers

Variadic parameters in Golang templates

Is there any way to "spread" parameters in Go templates? In my code I have a function that returns the properties of a list of structs as a string slice "colStr": func(col string, a ...interface{}) []string { s := make([]string, len(a)) for…
Brian Leishman
  • 8,155
  • 11
  • 57
  • 93
6
votes
3 answers

Prevent "" being inserted by golang text/template library

Go places as the result of a the template expansion when no value is present for a particular template parameter. Is there any way to prevent this? All I can think of right now is to insert an empty version of "AppVersion" into the data…
hookenz
  • 36,432
  • 45
  • 177
  • 286
6
votes
3 answers

How to pass multiple values from template to template?

My City struct is like this: type City struct { ID int Name string Regions []Region } And Region struct is: type Region struct { ID int Name string Shops []Destination …
user9314969
6
votes
1 answer

Iterate over only the first n items of an array in a Go template

I have a vector with n elements. I'm using it to render items in a template. But I need to render only the 5 first elements. Please note that is possible to have less than 5 elements in vector, in this case will render all elements. Is there is a…
André G. Andrade
  • 501
  • 11
  • 21
6
votes
3 answers

Golang unexpected EOF with nested templates

I am trying to display the contents of a slice on a page. Displaying the templates with static text works. As soon as I try and range over the slice sent to ExTpl(), I get an EOF error. type Miner struct { IP string `bson:"ip"` Port…
ChaChaPoly
  • 1,811
  • 5
  • 17
  • 39
6
votes
2 answers

How to parse multiple strings into a template with Go?

Is there a simple way like template.ParseFiles("base.html", "home.html") but for strings to build a template from a set of strings? I have a base template and a list of pages templates (all as strings) that I want to build on top of base template. I…
Sergei Basharov
  • 51,276
  • 73
  • 200
  • 335
6
votes
3 answers

Using methods with multiple return values

I'm trying to write a template (using html/template) and passing it a struct that has some methods attached to it, many of which return multiple values. Is there any way of accessing these from within the template? I'd like to be able to do…
6
votes
2 answers

text/template issue Parse() vs. ParseFiles()

I'm trying to do some simple work with the text/template package. The sample given at the top of template is what I'm working with. How do I write the 'parsed' file so template.ParseFiles() properly reads and executes it? package main import ( …
Evan Plumlee
  • 325
  • 1
  • 4
  • 7
5
votes
1 answer

Helm 'if' condition in one line

I was trying to put the if condition in a single line of the helm template: - name: ENV_VARIABLE value: {{- if .Values.condition }}"Value1"{{- else }}"Value2"{{- end }} However, I'm getting error: Error: YAML parse error on…
Danubian Sailor
  • 1
  • 38
  • 145
  • 223