This is what JSX is great at, and it's why I wrote a new web framework that basically just uses JSX for server-side views, that work the way I always thought JSX should: it just returns an object that you can then render into a string if you want or transform first. This is also how I made it so you can put <script> and <style> tags into any component, and it'll de-dup them before rendering, so that you can keep your component scripts and HTML close by each other in source.