Devasia Joseph
Devasia Joseph

Reputation: 388

Golang template function returns blank page

I am trying to change the default delimiters for Golang hml templates and here is the code I am using now:

func indexHandler(w http.ResponseWriter, r *http.Request) {
  pageFile := "html/testpage.html"
  tmpl, err := template.New(pageFile).Delims("[[", "]]").ParseFiles(pageFile)
  //tmpl := template.Must(template.ParseFiles(pageFile))
  if (err!=nil){
    fmt.Println("Error")
    fmt.Println(err)
  }

  tmpl.Execute(w, nil)
}

The above code renders a blank page in the browser. It will render properly if I use the commented out code instead of the second line.

Here is the template page source:

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8">

  <title>The HTML5 </title>
  <meta name="description" content="HTML5">
  <meta name="author" content="Test">   
</head>    
<body>
  This is html page
</body>
</html>

My go version is: go version go1.10.2 linux/amd64

I run it by go run test.go test.go being in the main package

There is no error message being printed in the browser or the terminal.

What am I missing here?

Upvotes: 3

Views: 4265

Answers (3)

Victor
Victor

Reputation: 3978

After some research and discussions i guess this line is ill formed:

tmpl, err := template.New(pageFile).ParseFiles(pageFile)

You don't need to do a New(pageFile). You only need to use the ParseFiles method directly and bear in mind that the name of the templaes will be equals to the base names of the passed files.

So, touch a little bit yhe code and use:

tmpl, err := template.ParseFiles(pageFile)

See this example for more

Upvotes: 4

SHURONG XU
SHURONG XU

Reputation: 1

func indexHandler(w http.ResponseWriter, r *http.Request) {
  pageFile := "html/testpage.html"
  name := "testpage"
  tmpl, err := template.New(name).Delims("[[", "]]").ParseFiles(pageFile)  //only translate a "name" to New()
  //tmpl := template.Must(template.ParseFiles(pageFile))
  if (err!=nil){
    fmt.Println("Error")
    fmt.Println(err)
  }

  tmpl.Execute(w, nil)
  //tmpl.ExecuteTemplate(w, name, nil)
}

Upvotes: 0

mkopriva
mkopriva

Reputation: 38313

Since html/template uses text/template underneath, you can often find additional information regarding how templates work in the text/template package.

From the docs of ParseFiles:

Since the templates created by ParseFiles are named by the base names of the argument files, t should usually have the name of one of the (base) names of the files. If it does not, depending on t's contents before calling ParseFiles, t.Execute may fail. In that case use t.ExecuteTemplate to execute a valid template.

(emphasis mine)


The problem is caused by the fact that you are passing the template file's path as the name of the template and then calling the ParseFiles method.

Because of how ParseFiles, and ParseGlob for that matter, are implemented, this causes an inconsistency between the name you explicitly passed to New and the names these two methods assign to the parsed templates.

You can test this by calling the DefinedTemplates method.

https://play.golang.org/p/LEi-xSn4LOF


Also please take a look at @icza's Go template name answer to get a better understanding of templates.

Upvotes: 3

Related Questions