templ icon indicating copy to clipboard operation
templ copied to clipboard

Ability to use templ.Attributes as variable inside templ function

Open andradei opened this issue 2 years ago • 0 comments

It would be great if we could set attrs := templ.Attributes{...} in the templ body. Today this throws an error

templ button(text, url, method string) {
	var hxMethod string
	switch method {
		case "get":
			hxMethod = "hx-get"
		case "post":
			hxMethod = "hx-post"
		case "delete":
			hxMethod = "hx-delete"
		default:
			hxMethod = ""
	}


	attrs := templ.Attributes{ 
		hxMethod: url,          // <--------- ERROR HERE: source formatting error: 352:11: missing ',' in argument list (and 2 more errors)
		"hx-swap": "outerHTML",
		"class": "my-2 px-4 bg-slate-200 rounded text-xl shadow-[0px_3px_0px_0px_rgba(72,72,72)]",
	 }
	<button { attrs... }>{ text }</button>
}

andradei avatar Dec 30 '23 08:12 andradei