templ icon indicating copy to clipboard operation
templ copied to clipboard

feat: ComponentScript spread attribute support

Open alehechka opened this issue 11 months ago • 1 comments

Overview

For context, there were a couple threads (1, 2) in the templ slack channel asking about support for ComponentScript attributes in the spread attribute functionality. This was just a quick exploratory attempt at this feature so I'm open to suggestions on the resulting generated code. The resulting HTML after rendering is effectively the same as if the script function were passed in directly instead of through a spread attribute.

The existing implementation only renders <script> elements from script components when directly provided in an ExpressionAttribute with the generated code as follows.

templ_7745c5c3_Err = templ.RenderScriptItems(ctx, templ_7745c5c3_Buffer, Alert("hello, world"))
if templ_7745c5c3_Err != nil {
	return templ_7745c5c3_Err
}

This proposed change will provide all spread attributes used within the element to a new RenderScriptItemsWithSpread that takes in a slice of Attributes and on runtime will loop over all attributes to discover any script components to render.

templ_7745c5c3_Err = templ.RenderScriptItemsWithSpread(ctx, templ_7745c5c3_Buffer, []templ.Attributes{attrs})
if templ_7745c5c3_Err != nil {
	return templ_7745c5c3_Err
}

Upon opening this PR, the implementation is set up so that providing spread attributes at all to an element will cause it to generate code with the new RenderScriptItemsWithSpread due to the unknown of what parameters might be passed in through the spread.

alehechka avatar Mar 06 '24 04:03 alehechka