12

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"))
    mux.HandleFunc("/", func(rw http.ResponseWriter, req *http.Request) {
        varmap := map[string]interface{}{
            "var1": "value",
            "var2": 100,
        }
        tmpl.ExecuteTemplate(rw, "index", varmap)
    })

    // content of index.html
    {{define "index"}}
    {{var1}} is equal to {{var2}}
    {{end}}
}
icza
  • 389,944
  • 63
  • 907
  • 827
Timur Fayzrakhmanov
  • 17,967
  • 20
  • 64
  • 95

3 Answers3

14

Yes just use the dot in front of it:

http://play.golang.org/p/7NXu9SDiik

package main

import (
    "html/template"
    "log"
    "os"
)

var tmplString = `    // content of index.html
    {{define "index"}}
    {{.var1}} is equal to {{.var2}}
    {{end}}
`

func main() {
    tmpl, err := template.New("test").Parse(tmplString)
    if err != nil {
        log.Fatal(err)
    }
    varmap := map[string]interface{}{
        "var1": "value",
        "var2": 100,
    }
    tmpl.ExecuteTemplate(os.Stdout, "index", varmap)

}
fabrizioM
  • 46,639
  • 15
  • 102
  • 119
8

It is possible to pass just a simple value of any type. If you do so, you can refer to it in the template as {{.}}.

Here is an example (try it on the Go Playgound):

s := "<html><body>Value passed: {{.}}</body></html>\n"

tmpl := template.Must(template.New("test").Parse(s))

tmpl.Execute(os.Stdout, false)
tmpl.Execute(os.Stdout, 1)
tmpl.Execute(os.Stdout, "me")

Output:

<html><body>Value passed: false</body></html>
<html><body>Value passed: 1</body></html>
<html><body>Value passed: me</body></html>
icza
  • 389,944
  • 63
  • 907
  • 827
  • 1
    But what if I want also to pass a struct with some variables? Is it possible? – Timur Fayzrakhmanov Jan 15 '15 at 19:27
  • 2
    @TimurFayzrakhmanov Then you should add the extra value to your struct, or create a new wrapper struct or add both the struct and the extra value to a map. You can only specify one value, but that one value can be a wrapper of arbitrary many other values. – icza Jan 15 '15 at 19:29
  • 1
    What about this? varmap := map[string]interface{}{ "var1": "value", "var2": 100, "var3": Struct }, Then call the struct members by .var3.StructMember ? – Timur Fayzrakhmanov Jan 15 '15 at 19:34
  • 1
    @TimurFayzrakhmanov Yes, in that case you could refer to it by `.var3.StructMember`. – icza Jan 15 '15 at 19:35
  • 1
    @TimurFayzrakhmanov Just so you know this "serial upvoting" is referred to as [**voting fraud**](http://stackoverflow.com/help/serial-voting-reversed) and is not allowed on Stack Overflow. And it is automatically detected and undone. – icza Jan 16 '15 at 03:40
  • sad (...I thought as much, but still it's quite logical behavior. – Timur Fayzrakhmanov Jan 16 '15 at 07:26
  • @TimurFayzrakhmanov your comment was helpful in terms of bundling an existing struct, and some extra values for populating input fields. – farhany Mar 27 '18 at 23:30
0

Try fasttemplate [1]. It accepts a map of placeholders' substitution values. And it works faster than html/template on simple templates.

[1] http://github.com/valyala/fasttemplate

valyala
  • 11,669
  • 1
  • 59
  • 62