Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
kostyay opened this issue Mar 4, 2021 · 2 comments
Closed
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@kostyay
Copy link

kostyay commented Mar 4, 2021

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 '-'

@mvdan
Copy link
Member

mvdan commented Mar 4, 2021

Identifiers (names) in Go can only contain alphanumeric characters and underscores; it seems sane to have the same restriction on text/template. It is a different syntax, but it's still aimed at being Go-like.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 4, 2021
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Apr 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants