Is it possible to have nested templates in Go using the standard library? (Google App Engine)

golang template json
golang template eq
golang template loop
go template substring
golang template nested struct
go template yaml
golang html/template
golang serve html/template

How do I get nested templates like Jinja has in the python runtime. TBC what I mean is how do I have a bunch of templates inherit from a base templates, just filing in blocks of the base templates, like Jinja/django-templates does. Is it possible using just html/template in the standard library.

If that is not a possibility, what are my alternatives. Mustache seems to be an option but would I then be missing out on those nice subtle features of html/template like the context sensitive escaping etc.? What other alternatives are ther?

(Environment: Google App Engin, Go runtime v1, Dev - Mac OSx lion)

Thanks for reading.


Yes it is possible. A html.Template is actually a set of template files. If you execute a defined block in this set, it has access to all the other blocks defined in this set.

If you create a map of such template sets on your own, you have basically the same flexibility that Jinja / Django offers. The only difference is that the html/template package has no direct access to the file system, so you have to parse and compose the templates on your own.

Consider the following example with two different pages ("index.html" and "other.html") that both inherit from "base.html":

// Content of base.html:
{{define "base"}}<html>
  <head>{{template "head" .}}</head>
  <body>{{template "body" .}}</body>
</html>{{end}}

// Content of index.html:
{{define "head"}}<title>index</title>{{end}}
{{define "body"}}index{{end}}

// Content of other.html:
{{define "head"}}<title>other</title>{{end}}
{{define "body"}}other{{end}}

And the following map of template sets:

tmpl := make(map[string]*template.Template)
tmpl["index.html"] = template.Must(template.ParseFiles("index.html", "base.html"))
tmpl["other.html"] = template.Must(template.ParseFiles("other.html", "base.html"))

You can now render your "index.html" page by calling

tmpl["index.html"].Execute("base", data)

and you can render your "other.html" page by calling

tmpl["other.html"].Execute("base", data)

With some tricks (e.g. a consistent naming convention of your template files), it's even possible to generate the tmpl map automatically.

app.yaml Configuration File, As much as I like the Go language and its standard library documentation, I have always had difficulty grasping exactly how to use the standard  129 Is it possible to have nested templates in Go using the standard library? (Google App Engine) Jul 13 '12 104 Why is my webserver in golang not handling concurrent requests?


note, when you execute your base template, you must pass values down to the child templates, here I simply pass ".", so that everything is passed down.

template one displays {{.}}

{{define "base"}}
<html>
        <div class="container">
            {{.}}
            {{template "content" .}}
        </div>
    </body>
</html>
{{end}}

template two displays {{.domains}} that's passed into the parent.

{{define "content"}}
{{.domains}}
{{end}}

Note, if we used {{template "content" .}} instead of {{template "content" .}}, .domains wouldn't be accessible from the content template.

DomainsData := make(map[string]interface{})
    DomainsData["domains"] = domains.Domains
    if err := groupsTemplate.ExecuteTemplate(w, "base", DomainsData); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }

Using Nested Templates in Go for Efficient Web Development, of " in the example above appears on standard output when the program is run. Comments do not nest and must start and end at the delimiters, as shown here. {{template "name"}} The template with the specified name is executed with nil data. Note that if there is only one variable, it is assigned the element; this is  The App Engine standard environment is based on container instances running on Google's infrastructure. Containers are preconfigured with one of several available runtimes. The App Engine standard environment makes it easy to build and deploy an application that runs reliably even under heavy load and with large amounts of data.


Use Pongo, which is a super-set of Go Templates that supports the {{extends}} and {{block}} tags for template inheritance, just like Django.

template - The Go Programming Language, The documentation here focuses on the security features of the package. HTML templates treat data values as plain text which should be encoded so Package html/template looks through template calls to compute the // context. This is usually an error and there are better solutions; for // example // <script>​alert("{{. 84 Is it possible to have nested templates in Go using the standard library? (Google App Engine) (Google App Engine) 23 Is there a decent alternative to Yahoo Pipes?


Handlebars, Handlebars is largely compatible with Mustache templates. In most cases it is possible to swap out Mustache with Handlebars and continue using your current​  I would like to run MIP solver on GAE/Go standard environment. I found GO MIP solvers like Goop and Golp.But these solvers requires external library, and can not run on Google App Engine Standard environment.


having worked with other template packages, now a days I mostly work with standard html/template package, I guess I was naive to not appreciate the simplicity it provides and other goodies. I use a very similar approach to accepted answer with following changes

you don't need to wrap your layouts with additional base template, a template block is created for every parsed file so in this case it is redundant, I also like to use the block action provided in new version of go, which allows you to have default block content in case you don't provide one in child templates

// base.html
<head>{{block "head" .}} Default Title {{end}}</head>
<body>{{block "body" .}} default body {{end}}</body>

and you page templates can be the same as

// Content of index.html:
{{define "head"}}<title>index</title>{{end}}
{{define "body"}}index{{end}}

// Content of other.html:
{{define "head"}}<title>other</title>{{end}}
{{define "body"}}other{{end}}

now to execute the templates you need to call it like so

tmpl["index.html"].ExecuteTemplate(os.Stdout, "base.html", data)

Awesome Go, EasyMIDI - EasyMidi is a simple and reliable library for working with standard midi "hard things" as possible so that each time you start a new web project in Go, Google App Engine - GAE). sessiongate-go - Go session management using make lightweight ASCII line graph ╭┈╯ in command line apps with no other  Go and Google App Engine. David Symonds, Nigel Tao, and Andrew Gerrand 10 May 2011 Google’s App Engine provides a reliable, scalable, easy way to build and deploy applications for the web. Over a hundred thousand apps are hosted at appspot.com and custom domains using the App Engine infrastructure.


Mako Templates, Mako is a template library written in Python. It is the default template language included with the Pylons and Pyramid web frameworks. Standard template features block. can access variables from their enclosing scope as well as the template's request context; can be nested arbitrarily Supports Google App Engine. 84 Is it possible to have nested templates in Go using the standard library? (Google App Engine) (Google App Engine) 23 Is there a decent alternative to Yahoo Pipes?


HTML's New Template Tag: standardizing client-side templating , It defines a new <template> element which describes a standard DOM-based approach for else { // Use old templating techniques or libraries. }  There has been a lot of discussion of this exact subject on the Objecify list in google groups , including several links to the Google documentation on the subject. Basically, any kind of query which does not include an ancestor() may return results from a stale view of the datastore.


Charts, Helm reserves use of the charts/ , crds/ , and templates/ directories, and of the listed The type field, discriminating application and library charts (see Chart Types). There can also be separate license(s) for the application installed by the chart, Template files follow the standard conventions for writing Go templates (see  For your convenience, App Engine includes the Django and Jinja2 templating engines. Using Jinja2 Templates The app.yaml file lists the latest version of jinja2 as a required library.