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

Make nested html! invocations lazy #381

Open
mattfbacon opened this issue Jun 5, 2023 · 4 comments
Open

Make nested html! invocations lazy #381

mattfbacon opened this issue Jun 5, 2023 · 4 comments

Comments

@mattfbacon
Copy link
Contributor

Related to #373.

For patterns like this:

fn body() -> Markup { ... }

html! {
  h1 { "My page" }
  main { (body()) }
}

There is technically no need for the intermediate buffer from body, because the markup could render directly into the outer buffer.

In my ideal API, Markup would just implement Render (the render method would have to return something other than Markup, probably just PreEscaped<String>) and take advantage of the existing render_to method.

@nikvoid
Copy link

nikvoid commented Jul 17, 2023

In my project (using #373) I solved it like this:

/// Helper for writing nested html_to!
/// Basically a lazy html! that can be rendered(-to) on demand
macro_rules! html_in {
    ($($tt:tt)*) => {
        MaudFnWrapper(|buf: &mut String| maud::html_to!{ buf, $($tt)* })
    };
}

/// A bit of closure magic to work around nested html_to!
struct MaudFnWrapper<F>(F);
impl<F> Render for MaudFnWrapper<F> 
where F: Fn(&mut String) {
    fn render_to(&self, buffer: &mut String) {
        self.0(buffer)
    }
}

Howerer this is mainly a workaround, than proper solution.

@mattfbacon
Copy link
Contributor Author

Cool! I'll take a look at that and maybe make a PoC of my idea.

@imbolc
Copy link
Contributor

imbolc commented Oct 31, 2024

Implemented in Hypertext. How difficult would it be to use the same approach in Maud?

@lambda-fairy
Copy link
Owner

It's probably fine. I think I was originally concerned about ergonomics around lifetimes, but the fact that other libraries don't care, as well as improvements on the Rust side, makes that concern unfounded. We'll still need to decide between Fn / FnMove though, and I'd like to get #412 landed first.

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

No branches or pull requests

4 participants