Rendering HTML elements from Go Templates

2,457 views
Skip to first unread message

Sankar P

unread,
Apr 21, 2012, 1:57:47 PM4/21/12
to golang-nuts, Sankar P
Hi,

I have the following code:

package main

import (
"net/http"
"html/template"
)

type data struct {
Data string
}

func handler(w http.ResponseWriter, req *http.Request) {
t := template.New("Test")
t, _ = t.Parse("<html><body>Hello {{.Data}}</body></html>")

val := data {Data:"<input type=text>"}
t.Execute(w, val)
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":1234", nil)
}

If I execute this code, the webpage displays: Hello <input type=text>
instead of Hello and a button.

What is the way to make HTML render from the go template output ?

Thanks

--
Sankar P
http://psankar.blogspot.com

minux

unread,
Apr 21, 2012, 2:06:42 PM4/21/12
to Sankar P, golang-nuts
On Sun, Apr 22, 2012 at 1:57 AM, Sankar P <sankar.c...@gmail.com> wrote:
package main

import (
       "net/http"
       "html/template"
)

type data struct {
       Data string
}

func handler(w http.ResponseWriter, req *http.Request) {
       t := template.New("Test")
       t, _ = t.Parse("<html><body>Hello {{.Data}}</body></html>")

       val := data {Data:"<input type=text>"}
       t.Execute(w, val)
}

func main() {
       http.HandleFunc("/", handler)
       http.ListenAndServe(":1234", nil)
}

If I execute this code, the webpage displays: Hello <input type=text>
instead of Hello and a button.

What is the way to make HTML render from the go template output ?
It seem that you don't want the safe html template, the easiest way to fix your problem is
to use "text/template" instead, just change your import and you're done.

Alternatively, we can make use of template.HTML as type of your data.Data.

Sankar P

unread,
Apr 21, 2012, 2:10:40 PM4/21/12
to minux, golang-nuts

Thank you a tonne :)

The templates thing is so under-documented, and most of the online
blog posts are out-dated. Your email helped me fix the problem that I
am stuck on. It works perfect now. Thanks a lot.

Shaban Naasso

unread,
Apr 21, 2012, 4:53:40 PM4/21/12
to golan...@googlegroups.com
just in case you ever want safe output you could simply do this.

import (
       "net/http"
       "html/template"
)

type data struct {
       Data string
}

// this function sends your string as HTML which is just a typed string
func(d *data)HTMLData()template.HTML{
 return template.HTML(d.Data)
}

func handler(w http.ResponseWriter, req *http.Request) {
       t := template.New("Test")
       t, _ = t.Parse("<html><body>Hello {{.HTMLData}}</body></html>")// and another change here


       val := data {Data:"<input type=text>"}
       t.Execute(w, val)
}

func main() {
       http.HandleFunc("/", handler)
       http.ListenAndServe(":1234", nil)
}

of course in your example doesn't make sense because it's overkill but i think you are practicing for something bigger so might come in handy at some point.
the idea is that simply there is a difference between HTML and a string.

good luck :-)

mibitzi

unread,
Apr 21, 2012, 6:35:01 PM4/21/12
to golan...@googlegroups.com, Sankar P
You can use the HTML type  http://golang.org/pkg/html/template/#HTML 
Like so:

val := data {Data:template.HTML("<input type=text>")} 
Reply all
Reply to author
Forward
0 new messages