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
10
votes
1 answer

same code but different results using gin + go-template

Basic information Go version: go1.4.2 darwin/amd64 Operating System: Mac OS X 10.10.5 I'm working on a small Web project written based on go and gin. Here is my golang code. After running go run test.go we have a web server, which is listening on…
Miaonster
  • 1,482
  • 2
  • 18
  • 32
9
votes
4 answers

helm chart template: if value does not exist, defaults to true

I am trying to declare that a block of code in a helm template should be present if a variable is true OR does not exist (ie, the default of the var is true). The following works: {{- if or .Values.livenessProbe (not (hasKey .Values…
Oliver
  • 27,510
  • 9
  • 72
  • 103
9
votes
2 answers

<.Chart.Name>: can't evaluate field Chart in type string

When I deploy the following I get this error: {{- if .Values.front.ingress.enabled -}} {{- $fullName := include "marketplace.fullname" . -}} {{- $ingressPaths := .Values.front.ingress.paths -}} apiVersion: extensions/v1beta1 kind: Ingress metadata: …
RuBiCK
  • 805
  • 4
  • 12
  • 23
9
votes
2 answers

How to have short circuit with and/or in text/template

I have this Go template: {{ if and $b.Trigger $b.Trigger.Name }} Name is {{ $b.Trigger.Name }}. {{ else }} ...other stuff... {{ end }} I'm trying to get this template to do: if b.Trigger != nil && $b.Trigger.Name != "" { ... however it…
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
9
votes
2 answers

How do you add the current year to a Go template?

In a Go template, you can retrieve a field like this: template.Parse("{{ .Title }}") template.Execute(w, myObject) How would you "inline" the current UTC year? I want to do something like…
BlueSky
  • 1,449
  • 1
  • 16
  • 22
9
votes
1 answer

Golang use json in template directly

I'm looking for a way of binding json data directly into a template (without any struct representation in golang) - and I'm coming up short. Essentially, what I want is to have both template document and json to be just arbitrary data - and my…
Darren Oakey
  • 2,894
  • 3
  • 29
  • 55
9
votes
2 answers

Go - HTML comments are not rendered

I'm building go web application. I found some anomaly on the rendered html page. All of my html comments are suddenly not being rendered. My guess it's because the go version I used (just updated to higher version), because it was fine…
novalagung
  • 10,905
  • 4
  • 58
  • 82
9
votes
1 answer

Using conditions inside templates

The use of if statements inside templates really is puzzling me. I'm trying to put a class = "active" inside a nav list made with golang templates, to do a basic tab menu that detects the active tab. Here's my attempt : {{define "header"}}
Nicolas Marshall
  • 4,186
  • 9
  • 36
  • 54
8
votes
2 answers

How to concatenate a variable and a string with the go templates

I need to concatenate a scoped variable and a string with go templates, something like that: {{ $url := .Release.Namespace + ".myurl.com" }} How can I do that?
Yagora
  • 108
  • 1
  • 1
  • 7
8
votes
2 answers

How to use if condition in helm chart

I have below values in values.yaml pg_hba: - hostssl all all 0.0.0.0/0 md5 - host all all 0.0.0.0/0 md5 The reqiurement is to check the hostssl line exists , if yes it should go into the if loop and do something. i tried to use {{ if…
Neelam Sharma
  • 99
  • 1
  • 2
  • 5
8
votes
1 answer

How to use templates in Go Gin for dynamic content

I have a simple Go / Gin web app. I need to put some dynamic content in html template. For e.g. I have a few tables (the number is dynamic) with a few rows (the number is dynamic). I need to put them in html template. Is there any way to combine…
kikulikov
  • 2,512
  • 4
  • 29
  • 45
8
votes
1 answer

It takes too much time when using "template" package to generate a dynamic web page to client in Golang

It is so slow when using template package to generate a dynamic web page to client. Testing code as below, golang 1.4.1 http.Handle("/js/", (http.FileServer(http.Dir(webpath)))) http.Handle("/css/",…
catric mia
  • 1,137
  • 2
  • 9
  • 8
8
votes
2 answers

Go alternative for python loop.last

I'm looking to loop over an array with Go templates and I want to add an extra string to the last item in the loop. In python, I can do {% for host in hosts %} {{ host }}{% if loop.last %} ;{% endif %} {% endfor %} Looking to achieve same thing…
oBa
  • 411
  • 3
  • 12
8
votes
1 answer

Presence of unrelated method in interface breaks text/template?

Playground link: http://play.golang.org/p/Ebf5AuJlcP type Foo interface {} type Bar interface { ThisIsABar() } // Person implements both Foo and Bar type Person struct { Name string } func (p Person) ThisIsABar() {} type FooContext…
Thomas
  • 174,939
  • 50
  • 355
  • 478
7
votes
2 answers

How to escape "{{" and "}}" in argo workflow

I want to run one argo workflow in which a value is surrounded with double braces. Argo tries to resolve it but I don't want argo to resolve it. Following is a fraction of katib studyjob workflow manifest. workerSpec: goTemplate: …
shabbir
  • 121
  • 2
  • 6