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 writing to existing html buffer #373

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nikvoid
Copy link

@nikvoid nikvoid commented Apr 12, 2023

Another attempt to add feature requested in #90.
This PR add html_to! macro that can use existing String as output buffer.
Example:

use maud::{self, html, html_to, Render};

#[test]
fn html_render_to_buffer() {
    let mut buf = String::new();

    html_to! { buf 
        p { "existing" }
    };
    
    assert_eq!(buf, "<p>existing</p>");
}

#[test]
fn html_buffer_reuse() {
    let mut buf = String::new();
    html_to! { buf 
        p { "existing" }
    };
    
    html_to! { buf 
        p { "reused" }
    };
    
    assert_eq!(buf, "<p>existing</p><p>reused</p>");
}

#[test]
fn impl_render_to_html_to() {
    struct Foo;
    impl Render for Foo {
        fn render_to(&self, buffer: &mut String) {

            html_to! { buffer
                a { "foobar" }                
            }
        }
    }

    let rendered = html! {
        p { (Foo) }
    }.into_string();
    
    assert_eq!(rendered, "<p><a>foobar</a></p>");
}

I'm requesting comments on how to improve this and fit it better into existing code

@nikvoid nikvoid changed the title PoC writing to existing buffer with html_to! macro Allow writing to existing html buffer Apr 12, 2023
@nikvoid
Copy link
Author

nikvoid commented Apr 12, 2023

Oh, just realized that this won't work simply when trying to interpolate Render value in html_to! because of &mut reference.
image

… to avoid taking &mut String references (fix Splicing exprs into html_to!)
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.

None yet

1 participant