Hi all,

I am building a Go web app, and am struggling to determine best-practice
for using html/template.

It seems from the example in text/template documentation that the
'recommended' way to use multiple templates to construct a page is along
the lines of:

base, _ := templates.New("base").Parse(` The message is: {{template

When I want to render my Hello World, I go:

temp, _ := base.Clone()
temp.Parse(` {{define "content"}}Hello, World{{end}} `)

temp.Execute(os.Stdout, nil)

But it seems to me that this involves a large quantity of parsing on each

I can do this:

hello, _ := templates.New("hello").Parse("Hello, World")
goodbye,_ := templates.New("goodbye").Parse("Goodbye, cruel world!")

And now decide what I want the user to see:

var temp *template.Template
render, _ := base.Clone()
if !pinkFloyd {
temp, _ = render.AddParseTree("content", hello.Tree)
} else {
temp, _ = render.AddParseTree("content", goodbye.Tree)
render.Execute(os.Stdout, nil)

This seems a little better, but it still involves Cloning the base
template, and it also involves using the text/template/parse/Tree in the
Template type, which is meant to be internal to the template package.

Of course, all this would be unnecessary if I could just go:

data := make(map[string]string)
data["Content"] = "hello"
base,_ := templates.New("base").Parse(`{{define "hello"}}Hello,
Word{{end}} {{define "goodbye"}}Goodbye, cruel world!{{end}} The message
is: {{template .Content}}`)

But that, alas, cannot be done (although if there is interest, I am
prepared to write such a patch).

Am I using text/template (html/template actually, of course) correctly?

Is there a better way to do this?

All the best,

You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 4 | next ›
Discussion Overview
groupgolang-nuts @
postedApr 19, '13 at 4:27a
activeApr 20, '13 at 2:10p

2 users in discussion

Craig Mason-Jones: 3 posts Dougx: 1 post



site design / logo © 2021 Grokbase