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

3

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}}
}
Marrowfat answered 15/1, 2015 at 19:16 Comment(0)
C
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)

}
Cufic answered 15/1, 2015 at 19:25 Comment(0)
C
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>
Castera answered 15/1, 2015 at 19:24 Comment(7)
But what if I want also to pass a struct with some variables? Is it possible?Marrowfat
@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.Castera
What about this? varmap := map[string]interface{}{ "var1": "value", "var2": 100, "var3": Struct }, Then call the struct members by .var3.StructMember ?Marrowfat
@TimurFayzrakhmanov Yes, in that case you could refer to it by .var3.StructMember.Castera
@TimurFayzrakhmanov Just so you know this "serial upvoting" is referred to as voting fraud and is not allowed on Stack Overflow. And it is automatically detected and undone.Castera
sad (...I thought as much, but still it's quite logical behavior.Marrowfat
@TimurFayzrakhmanov your comment was helpful in terms of bundling an existing struct, and some extra values for populating input fields.Narthex
P
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

Paluas answered 23/8, 2015 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.