golang: Django, Jinja like Template inheritance using "html/template" and how to unescape HTML in "html/template"

(Japanese version: here)



{{define "base"}}<!doctype html>
        <meta charset="utf-8">
        <title>{{template "title" .}}</title>

        <div class="content">
            {{template "body" .}}
        </div><!-- /.content -->


{{define "title"}}{{.Title}}{{end}}

{{define "body"}}


package main

import (

func viewHandler(w http.ResponseWriter, r *http.Request) {
    funcMap := template.FuncMap{
        "safehtml": func(text string) template.HTML { return template.HTML(text) },
    templates := template.Must(template.New("").Funcs(funcMap).ParseFiles("templates/base.html",
    dat := struct {
        Title string
        Body  string
        Title: "How do I search google Go pages?",
        Body:  "Use <b>Golang</b> instead of <i>Go</i>.",
    err := templates.ExecuteTemplate(w, "base", dat)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)

func main() {
    http.HandleFunc("/", viewHandler)
    http.ListenAndServe(":8080", nil)
go run templatetest.go

Open http://localhost:8080 in a browser.


  • Registering custom template methods using template.FuncMap{} and template.Funcs().
    Custom method is used in template/view.html at {{.Body|safehtml}}
  • Reading a child template like below:
    go template.ParseFiles("templates/base.html", "templates/view.html")
  • Set only "base" scope like below:
    go templates.ExecuteTemplate(w, "base", dat)


var := template.HTML(var) in Go can be used like a Django's method var = mark_safe(var).

I could use template.HTML() but it was hard to convert type when ORM library save models. Then I want a method like a Django and Jinja's {{var|safe}} that can be used at a time to display raw HTML code.

By the way, We can disable autoescape mode as default using text/template instead of html/template. While using it, we can escape code with html.EscapeString() as needed. But it's not secure as way of thinking on safety engineering.