Skip to content

Add support for spreading non-string attribute values in templ.RenderAttributes #1212

@cuboci

Description

@cuboci

Describe the bug

Since merging #1129 it's allowed to do something like this

<input type="text" value={ val } />

where val is an int. It will get converted automatically and rendered as

<input type="text" value="42">

However, if I do this

{{ attr := templ.Attributes{"type": "text, "value": 42} }}
<input { attr... } />

only the type attribute will get rendered:

<input type="text">

The culprit seems to be RenderAttributes in runtime.go, which in its switch statement only type-checks for string, *string, bool, *bool, KeyValue[string, bool], KeyValue[bool, bool] and func() bool.

Expected behavior

I'd expect attribute values stored in templ.Attributes (which is a map[string]any after all) to be treated the same as if I had specified them directly, that is passed through fmt.Sprint or something.

templ info output

(✓) os [ goos=linux goarch=amd64 ]
(✓) go [ location=/usr/local/go/bin/go version=go version go1.25rc1 linux/amd64 ]
(✓) gopls [ location=/home/chris/.local/bin/gopls version=golang.org/x/tools/gopls v0.19.1 ]
(✓) templ [ location=/home/chris/.local/bin/templ version=v0.3.906 ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions