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

Concatenate variable in string

How can I concatenate a variable in a string that is used a Go template? This is the original line (from https://github.com/nginx-proxy/nginx-proxy/blob/master/nginx.tmpl): {{ $host := trim $host }} {{ $is_regexp := hasPrefix "~" $host }} {{…
brgsousa
  • 333
  • 1
  • 7
  • 20
-2
votes
1 answer

Golang template variable

This is my first stab at doing front end with Go. I have this template: {{define "index"}}{{template "_header.html"}} {{template "_message.html"}}
Page Index
Welcome {{.Title}}
{{template "_footer.html"}} …
pigfox
  • 1,301
  • 3
  • 28
  • 52
-2
votes
1 answer

using variables of one function in other function in go lang

package main import ( "encoding/json" // "flag" //"fmt" "strconv" "html/template" "time" // "log" "strings" "net/http" "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" …
Sai Vamsi
  • 111
  • 3
  • 19
-2
votes
1 answer

"can't evaluate field key in type interface" accessing Keys in Go template

My template: {{ .title }}

New Id On My Website

{{/* key, val */}}{{ range .lead }}
{{ .key…
Pardha.Saradhi
  • 468
  • 1
  • 10
  • 27
-2
votes
1 answer

How to style Go errors with HTML and CSS?

I would like to style Errors in my Go Web Application. Currently I am handling errors like the following example: if !ok { http.Error(w, "Username and/or password do not match", http.StatusForbidden) return } However this causes the error…
Peter
  • 119
  • 12
-2
votes
1 answer

Text template not working with some words

I am building a CLI to generate code for an homemade API framework (right now to generate the controller part). To do so, I am using template, but I saw that the template is generating nothing (an empty file), when I use words such as package or…
Adrien Chapelet
  • 386
  • 4
  • 24
-2
votes
1 answer

Show variable image in Go templates

I'm using a template in a Go web application which should show an image depending on which country the visitor is from. For the images I use the FileServer http.Handle("/images/", http.StripPrefix("/images/",…
joske123
  • 35
  • 3
-2
votes
1 answer

No such template "xxx" in Golang HTML Web Apps

I'm learning about embedding HTML in Go. Then I get this message when I run the server.go template executing error: html/template:base.html:30:25: no such template "Sidebar" Here's my code Go-HTML-Template //server.go package main import ( …
Ulfah Putri
  • 41
  • 2
  • 12
-2
votes
2 answers

How to get key from map

I'm working on go template. Having some map in . I know how to get the value, as long as I know the key. "Map value: {{ printf "%s" .key1 }}" How to get key name from inside the template? I would expect maybe something like "Map key: {{ printf "%s"…
BartBiczBoży
  • 2,512
  • 2
  • 24
  • 33
-2
votes
1 answer

ExecuteTemplate (template.ParseGlob) loads Blank page

I'm trying to load template page.gohtml from folder cms/template. When trying to view in Firefox browser it load Blank. handler.go file var Tmpl = template.Must(template.ParseGlob("../templates/*")) func ServeIndex(w http.ResponseWriter, r…
STEEL
  • 8,955
  • 9
  • 67
  • 89
-2
votes
1 answer

When serving .html templates in go, it does not load any media

I am currently working on a Go web server utilizing the builtin templates. The current issue I am having is that when I run the web server, it is serving the correct files but it does not load any media on the site.(Such as pictures and fonts) When…
Ethan
  • 108
  • 2
  • 9
-3
votes
1 answer

How to make a unique id for and

With {{ define "something" }} we can reuse some html code: {{ template "something" . }} {{ template "something" . }} {{ template "something" . }} And this is great but sometimes (honestly pretty often) we need use id's. The most common case is when…
Dmitry Gashko
  • 170
  • 2
  • 6
-3
votes
1 answer

Can't add time to date in Google go without parse error

Works: {{ $temp := timestampToDate $var.date }} {{ $temp.Format 2006/01/02 }} Doesn't work {{ $temp := timestampToDate $var.date }} {{ $temp := $temp.AddDate(0,-1,0) }} {{ $temp.Format 2006/01/02 }} It says it can't parse the file with the…
not_a_generic_user
  • 1,906
  • 2
  • 19
  • 34
-3
votes
1 answer

Template passes non-nil object becomes nil in frontend

basically I have an object wanted to pass to the frontend. I logged it in the backend and it was not null, but in the frontend when I alerted it, it becomes null. ... presentation := &presentationStruct { Object: object, } log.Errorf("%v", object)…
Haha
  • 31
  • 6
1 2 3
51
52