Error parsing html template

794 views
Skip to first unread message

Stephanie Sherriff

unread,
Apr 12, 2015, 10:30:08 PM4/12/15
to golan...@googlegroups.com
Hi,

I'm trying to parse an html file to be used as an html email.

I'm able to successfully parse html as a string within the code, but I'd rather create the html in a file, and the parse that. I'm getting the following runtime error:

PANIC: runtime error: invalid memory address or nil pointer dereference

 

goroutine
26 [running]:

github
.com/codegangsta/negroni.func·003()

 
/Users/StephMacbook/Workarea/Go/src/github.com/codegangsta/negroni/recovery.go:34 +0x10d

html
/template.(*Template).escape(0xc208147440, 0x0, 0x0)

 
/usr/local/Cellar/go/1.4.2/libexec/src/html/template/template.go:59 +0xe4

html
/template.(*Template).Execute(0xc208147440, 0x836db8, 0xc208010d20, 0x390920, 0xc2081473e0, 0x0, 0x0)

 
/usr/local/Cellar/go/1.4.2/libexec/src/html/template/template.go:75 +0x3d


Here is the code:

var doc bytes.Buffer
var err error

t
:= template.New("emailTemplate")                           //create a new template

t
, err = t.ParseFiles("templates/" + templateName + ".html") //open and parse a template html file
//t, err = t.Parse(resetPasswordEmail)

if err != nil {
 fmt
.Println("Error parsing template ", err.Error())
}

err
= t.Execute(&doc, data) //merge template ‘t’ with content

if err != nil {
 fmt
.Println("Error executing template ", err.Error())
}


If I comment out the ParseFiles and use just Parse instead it works correctly. I'm handling the error for t.Execute, but it isn't helping, even though the error is obviously from that line. 

It is finding the file, because if I change the path to the file, I get a specific error when calling ParseFiles that the file isn't found.

Thanks ahead of time for your help.

Steph


David Symonds

unread,
Apr 12, 2015, 11:01:07 PM4/12/15
to Stephanie Sherriff, golang-nuts
After you do the error check ater t.ParseFiles, you're just printing
out the error and continuing on. That's not valid; if t.ParseFiles
fails then the returned template is not usable.

I'm a bit surprised that t.Execute would panic in that way, except if
t.ParseFiles had failed. Can you create a self-contained example?

Stephanie Sherriff

unread,
Apr 13, 2015, 7:39:20 PM4/13/15
to David Symonds, golang-nuts
Hi David,

Thanks for the reply.

Yes, I know at the moment I'm just printing out the error, I haven't completely finished the functionality. At the moment I'm just trying to figure out why it is failing, so it was easier just to print out the error for now.

As far as I can tell t.ParseFiles is not failing, or if it is, it is failing silently without returning an error.

Things are pretty busy for the next day or so, but I'll put together a self-contained example as soon as I can. 

Cheers,
Steph

James Bardin

unread,
Apr 13, 2015, 10:30:35 PM4/13/15
to golan...@googlegroups.com
The panic is a bug that's fixed for go1.5.

You're actually creating 2 templates here under "t", one called emailTemplate which is empty, and one with the name of your template file. You either need to use the same name for both, or call ExecuteTemplate with the proper template name.

Stephanie Sherriff

unread,
Apr 15, 2015, 11:29:21 PM4/15/15
to James Bardin, golang-nuts
Excellent! Thanks very much James. I didn't see in the documentation that ParseTemplate actually creates a new template.

For anyone else who has this issue, this is the stripped down basics of what I've done:

func GenerateEmailHTML(templateName string, data interface{}) (string, error) {
var doc bytes.Buffer
var err error
var t *template.Template

t, err = t.ParseFiles("templates/" + templateName + ".html") //open and parse a template html file

if err != nil {  // do error stuff here, or log error, etc
fmt.Println("Error parsing template ", err.Error())
return "", err
} else {
err = t.Execute(&doc, data) //merge template ‘t’ with content
if err != nil {  // do error stuff here, or log error, etc
fmt.Println("Error executing template ", err.Error())
return "", err
} else {
s := doc.String()
fmt.Println(s)
return s, nil
}
}
}

Thanks for the help!

Cheers,
Steph

On Tue, Apr 14, 2015 at 12:30 PM, James Bardin <j.ba...@gmail.com> wrote:
The panic is a bug that's fixed for go1.5.

You're actually creating 2 templates here under "t", one called emailTemplate which is empty, and one with the name of your template file. You either need to use the same name for both, or call ExecuteTemplate with the proper template name.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/vmZHHfLsH8M/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages