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

Go templates range loop is quoting my value

I've got a slice of strings (.Table.PKey.Columns) that I'm trying to loop over in my template to generate a go file that does some appends, but when I output $value in my template, apparently Go is quoting it for me, so it is giving me the error:…
b0xxed1n
  • 2,063
  • 5
  • 20
  • 30
0
votes
1 answer

Rendering template in golang

I am using the echo framework in Go to create a web app. I have a directory called templates inside which I have two directories layouts and users. The directory tree is as…
rahules
  • 807
  • 3
  • 12
  • 33
0
votes
2 answers

Redirect requests for static files

I'm trying to serve a static html file, and this file has script tags that point to other resources. I want to serve the html file from one directory but then redirect requests for assets to another directory. This is how I'm setting it up now: //…
dopatraman
  • 13,416
  • 29
  • 90
  • 154
0
votes
1 answer

Go template include external css

index.go package main import ( "html/template" "net/http" ) func viewHandler(w http.ResponseWriter, r *http.Request) { t, _ := template.ParseFiles("index.html") t.Execute(w, nil) } func main() { http.Handle("/static/",…
Wyatt
  • 1,357
  • 5
  • 17
  • 26
0
votes
1 answer

System cannot find path specified trying to parse template

I'm just getting started learning about html/templates in Go. The error I'm getting is that 'system cannot find file path specified'. and the file path is templates/time.html. the location of time.html (the page I'm trying to render) is…
user2923605
  • 75
  • 1
  • 3
  • 11
0
votes
1 answer

Go template to struct

I have a Go template that should resolve to a struct. How can I convert the bytes.Bufferresult from template execute function back to the struct. Playground package main import ( "bytes" "encoding/gob" "fmt" "log" …
aminjam
  • 646
  • 9
  • 25
-1
votes
1 answer

Indentation and text formatting with go templates

Given the below template: {{ range $item := . }} {{ if $item.IsA}} Ok {{ else }} Fine {{ end }} {{ end }} Done! When I render it using: t := template.New("test").Parse(_types) text, err := t.Execute(&buffer,…
Arnold Zahrneinder
  • 4,788
  • 10
  • 40
  • 76
-1
votes
1 answer

How can i change the Delim in echo Render?

i am trying to change the delimiter for go in an html template. Unfortunately, it does not work in the render function, nor in the main function. See https://pkg.go.dev/text/template#Template.Delims package main import ( "html/template" …
Sienael
  • 69
  • 8
-1
votes
1 answer

Accessing docker-cli string array element in Go templates

I created a docker container but cannot seem to parse its properties with docker-cli and its format parameter (which uses Go templates). Any idea greatly appreciated. Start a docker container, e.g. go to…
Marcuse7
  • 57
  • 4
-1
votes
1 answer

How to submit form in go template without reloading the whole html?