Skip to content

Make nested html! invocations lazy #381

Open

Description

@mattfbacon

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.

Activity

  1. nikvoid commented on Jul 17, 2023

    @nikvoid

    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.

  2. mattfbacon commented on Jul 17, 2023

    @mattfbacon
    ContributorAuthor

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

  3. imbolc commented on Oct 31, 2024

    @imbolc
    Contributor

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

  4. lambda-fairy commented on Jan 6, 2025

    @lambda-fairy
    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.

  5. StratusFearMe21 commented on Nov 11, 2025

    @StratusFearMe21

    I just made a PR (#484) that would fix this

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions