Skip to content

text/template: Why is there no support for "kebab-case" template variables? #44789

@kostyay

Description

@kostyay

Hi
Is there a particular reason template variables can't contain dashes?

I'm aware of the index function. Just wondering why not support templates with kebab case directly using the prettier syntax.

The fix seems pretty simple in
template/parse/lex.go

// isAlphaNumeric reports whether r is an alphabetic, digit, or underscore.
func isAlphaNumeric(r rune) bool {
	return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r) || r == '-'
}

What side effects can this change have?
Thanks.

What version of Go are you using (go version)?

$ go version
1.16

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env

What did you do?

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

package main

import (
	"text/template"
	"os"
)

func main() {

	vals := map[string]string{"hello-world": "world!"}

	tmpl, err := template.New("test").Parse("{{ .hello-world }} items")
	if err != nil { panic(err) }
	err = tmpl.Execute(os.Stdout, vals)
	if err != nil { panic(err) }
}

What did you expect to see?

world! items

What did you see instead?

panic: template: test:1: bad character U+002D '-'

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions