hypertext icon indicating copy to clipboard operation
hypertext copied to clipboard

Include an .html file wholesale

Open biruburu opened this issue 4 months ago • 1 comments

Is there a way to split the rsx content into a dedicated .html file and include it to be parsed in hypertext? Based on the rsx-htmx example, something like this:

document.rs:

use hypertext::prelude::*;

use crate::views::nav::Nav;

#[component]
pub fn document<'a, R: Renderable>(selected: &'a str, children: &R) -> impl Renderable {
    rsx!("document.html")
}

document.html:

<!DOCTYPE html>
<html>

<head>
    <title>"Hypertext - HTMX with RSX"</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://unpkg.com/htmx.org@2"></script>
    <link rel="stylesheet" href="/styles.css">
</head>

<body class="bg-gray-900 text-gray-100">
    <h1 class="flex text-5xl mx-auto font-bold justify-center items-center mb-2">Hypertext</h1>
    <Nav selected=selected oob=true />
    <div id="page" class="mt-2">
        (children)
    </div>
</body>

</html>

This would give html formatting for free, including tailwind class sorting and all the other niceties from modern LSPs, as well as satisfy people who are allergic to mixing html inside rs files. (I'm not one of them, but I'm working with people who are refusing to look at hypertext because of that...) sqlx::query_file!() does something similar.

I'm not a macro expert, but would be happy to contribute if something like this would be interesting for hypertext.

In any case, awesome job on hypertext, this is by far my favourite rust templating library in terms of ergonomics and performance!

biruburu avatar Aug 13 '25 18:08 biruburu

Thank you so much for the kind words, they mean a lot! :) this is actually possible pretty natively, and the macro can be simply created for use in your crate. would you be interested in me adding a similar macro to the library itself?

macro_rules! include_html {
	($($tt:tt)*) => {
        // XSS SAFETY: these are static html files included at compile time, verified by the developer
        ::hypertext::Raw::dangerously_create(include_str!($($tt)*)).rendered()
    };
}

edit: oh wait, oops i didn't realize you wanted it to have (expr)s and stuff as well. this is very possible and i can take a look at supporting it in the macro. :)

vidhanio avatar Aug 14 '25 19:08 vidhanio