Html template returns text instead of html

I’ve the below code:

package main

import (
	htmlTempl "html/template"

var templatesHtml *htmlTempl.Template
var err error

func init() {
	fmt.Println("Starting up.")
	templatesHtml = htmlTempl.Must(htmlTempl.ParseGlob("templates/*.html"))

func test(w http.ResponseWriter, r *http.Request) {
	err = templatesHtml.ExecuteTemplate(w, "other.html", nil)
	if err != nil {

func main() {
	server := http.Server{
		Addr: "",
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./public"))))
	http.HandleFunc("/test", test)

And my templates are:

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


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

The output I got at is:

enter image description here

While I was expecting normal HTML page to be displayed!

I found it, the error was in using // as comment markers, it had be solved by replacing them with the html content comment marker as below:

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

And it is working fine now.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.