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

feat: ComponentScript spread attribute support #595

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

alehechka
Copy link
Contributor

@alehechka alehechka commented Mar 6, 2024

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.

@@ -691,6 +695,20 @@ func (c ComponentScript) Render(ctx context.Context, w io.Writer) error {

// RenderScriptItems renders a <script> element, if the script has not already been rendered.
func RenderScriptItems(ctx context.Context, w io.Writer, scripts ...ComponentScript) (err error) {
return RenderScriptItemsWithSpread(ctx, w, nil, scripts...)
Copy link
Contributor Author

@alehechka alehechka Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mainly only went this route to keep RenderScriptItems unchanged to avoid a breaking change. But if that's not a concern we can just add the extra parameter to this function instead.

@alehechka alehechka mentioned this pull request Mar 15, 2024
@indaco
Copy link

indaco commented Apr 10, 2024

any update on this? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants