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

Allow script components to be rendered inline #285

Merged
merged 4 commits into from
Nov 13, 2023

Conversation

JustDerb
Copy link
Contributor

@JustDerb JustDerb commented Nov 5, 2023

Allows templ to render script components, which is useful for inline Javscript. As of now, script components can really only be used within HTML attributes. This extends the ! usage to now enable embedding Javascript outside HTML attributes.

This fixes #231

I've also added a test case to show how it would be used (and what would be rendered). I've ensured that things should be backwards compatible, the only things that new generator runs would introduce is the new CallInline struct member (which is only called if you use this new functionality).

Copy link
Owner

@a-h a-h left a comment

Choose a reason for hiding this comment

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

Brilliant. It's a better idea than what I had on how to achieve the same goal.

Well thought out and done. Just a few minor things on docs etc., and it's good to go. Thanks for your work on this.

docs/docs/03-syntax-and-usage/11-script-templates.md Outdated Show resolved Hide resolved
docs/docs/03-syntax-and-usage/11-script-templates.md Outdated Show resolved Hide resolved
runtime.go Outdated Show resolved Hide resolved
runtime.go Outdated Show resolved Hide resolved
runtime.go Outdated Show resolved Hide resolved
runtime.go Outdated Show resolved Hide resolved
docs/docs/03-syntax-and-usage/11-script-templates.md Outdated Show resolved Hide resolved
docs/docs/03-syntax-and-usage/11-script-templates.md Outdated Show resolved Hide resolved
runtime.go Show resolved Hide resolved
@JustDerb
Copy link
Contributor Author

Looks like switching to slices might have broken something... Will look on Sunday 👀

@a-h
Copy link
Owner

a-h commented Nov 11, 2023

Yes, that was a bad idea of mine then, sorry! :) thanks for looking into it!

@JustDerb
Copy link
Contributor Author

Alright, should be fixed!

@a-h a-h merged commit abe23c6 into a-h:main Nov 13, 2023
4 checks passed
@a-h
Copy link
Owner

a-h commented Nov 13, 2023

Thanks!

@seanmorton
Copy link

I literally just ran into this use case today while learning templ, what a coincidence and thanks for this!

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.

How to inject a standalone <script> tag with data injected
3 participants