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
13
votes
4 answers

How to execute a Golang template when "{" or "}" are in the static part of the template?

My Problem is that, I want to build a letter generator, which first builds a latex-file from user input, and then compiles this via latex to PDF. The template contains multiple lines like this: \setkomavar{signature}{{{.Name}}} The latex part is…
user2326871
  • 231
  • 1
  • 8
12
votes
1 answer

How to control indent after including file content in Helm configMap?

I'm setting up a ConfigMap for my Helm chart. As per good practice, I want to include non-yaml resources through separate files rather than inline. Currently I am trying to include both an xml file and a tpl helper in my ConfigMap under "data". Both…
Bluebluebleu
  • 153
  • 1
  • 2
  • 7
12
votes
1 answer

Kubernetes Helm Chart If Condition Check

I am trying to add if great than condition in Helm chart. it is throwing error. I have defined value in values.yaml and using that value in deployment.yaml for condition. values.yaml replicaCount: 2 deployment.yaml rollingUpdate: maxSurge: 1 {{…
Gnana
  • 2,130
  • 5
  • 26
  • 57
12
votes
1 answer

In a Hugo partial template, how do I access secondary/additional parameters?

Say I have {{ partial "li.html" $test $root.Data.Term }}. With this I can can access the first parameter, or $test, by simply refering to . within the li.html template, but how do I access the second or additional parameter ($root.Data.Term) from…
01AutoMonkey
  • 2,515
  • 4
  • 29
  • 47
12
votes
2 answers

Go templates with eq and index

Go templates have some unexpected results when using eq with index for me. See this code: package main import ( "os" "text/template" ) func main() { const myTemplate = ` {{range $n := .}} {{index $n 0}} {{if (index $n 0) eq (index…
Ondra
  • 3,100
  • 5
  • 37
  • 44
12
votes
3 answers

How to pass just a variable (not a struct member) into text/html template. Golang

is there any way to pass just a variable (string, int, bool) into template. For example (something similar): import ( "html/template" ) func main() { .... tmpl := template.Must(template.ParseFiles("templates/index.html")) …
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95
11
votes
2 answers

detect last item inside an array using range inside go-templates

This program outputs simply 1,4,2, but I would like to print 1,4,2. As you can see the comma is printed after each items of an array. package main import "os" import "text/template" func main() { params := map[string]interface{}{ …
sensorario
  • 20,262
  • 30
  • 97
  • 159
11
votes
1 answer

Compare two variables inside Go template

In the data I pass to my template I have the two variables Type and Res.Type I want to compare to preselect an option for my select field. To illustrate my problem I have created this simplified version: package main import ( "bufio" …
Tobias Meyer
  • 345
  • 1
  • 3
  • 10
11
votes
1 answer

range within range golang template

How to access range within range in Go templates? Template: {{range .Resume.Skills}} {{.Name}} {{.Level}} {{range $item, $key := .Keywords}} {{$key}} {{$item}} {{end}} {{end}} Struct: type SkillsInfo…
Mangirdas
  • 243
  • 1
  • 3
  • 9
11
votes
1 answer

Golang embed html from file

How can I do in Golang if I have an HTML file like this:
{{.Header}}
and I want to embed a part of code into to…
PumpkinSeed
  • 2,945
  • 9
  • 36
  • 62
10
votes
1 answer

Helm template - how to use "if exists at least one of" in array?

I'm trying to make a list of env vars from a values.yaml become a single secret.yaml file containing the list of envs of the type "secret". The idea is to only create this secret file if at least one of the types equals…
Edgar
  • 143
  • 1
  • 1
  • 8
10
votes
3 answers

kubectl apply error: error converting YAML to JSON

Getting this error message after kubectl apply -f . error: error converting YAML to JSON: yaml: invalid map key: map[interface {}]interface {}{"include (print $.Template.BasePath \"/configmap.yaml\") . | sha256sum":interface {}(nil)} I've tried…
Ali
  • 143
  • 1
  • 1
  • 10
10
votes
4 answers

How to indent content of included template

I am using go templates to create yaml definitions for kubernetes. I am trying to nest templates but run into issues where I can't re-use a definition simply because the indention is wrong when included. I.e., in one case the contents need…
dirtbikejunkie
  • 471
  • 1
  • 5
  • 5
10
votes
4 answers

Iterate Go map get index

In order to use revel's even keyword in templates I would like to get the index of a map entry when iterating with range. Is there any way to do so? My map has the structure: map[string][]string
DiCaprio
  • 823
  • 1
  • 7
  • 24
10
votes
1 answer

Golang template engine pipelines

I have a Golang template, defined like this {{- define "test" -}} {{- printf "%s" .Name | trunc 24 -}} {{- end -}} Then I use it in one of my files: {{ template "test" . }} What does the dot mean after "test"? Golang template docs say: {{template…
Rahul
  • 1,727
  • 3
  • 18
  • 33