Skip to content

text/template: confusing overlay template behavior when it starts with a BOM #28482

@bep

Description

@bep

The program below prints Master: overlay once, the second prints nothing. And no error. The program is a variation of this example: https://golang.org/pkg/html/template/#example_Template_block -- which is how we do template "inheritance" in Hugo. And judging by the issue reports in this area, adding a BOM marker seems to be a fairly common practice among text editors. I can work around this on my end, but this behavior is very surprising.

https://play.golang.org/p/MQRb5HUk6eH

package main

import (
	"fmt"
	"log"
	"os"
	"text/template"
)

func main() {
	for _, prefix := range []string{"", "\ufeff"} {
		var (
			master  = `Master: {{block "list" .}}block{{end}}`
			overlay = prefix + `{{define "list"}}overlay{{end}}`
		)

		masterTmpl, err := template.New("master").Parse(master)
		if err != nil {
			log.Fatal(err)
		}
		overlayTmpl, err := template.Must(masterTmpl.Clone()).Parse(overlay)
		if err != nil {
			log.Fatal(err)
		}

		if err := overlayTmpl.Execute(os.Stdout, ""); err != nil {
			log.Fatal(err)
		}

		fmt.Println()
	}
}

gohugoio/hugo#4895

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions