Add a simple `render_to_string()` helper for synchronous HTML rendering

This commit is contained in:
Greg Johnston 2022-11-22 19:51:05 -05:00
parent 3482f456f8
commit 19c3186d3f
1 changed files with 18 additions and 2 deletions

View File

@ -12,12 +12,28 @@ pub fn escape_attr(text: &str) -> Cow<'_, str> {
}
cfg_if! {
if #[cfg(feature = "ssr")] {
if #[cfg(any(doc, feature = "ssr"))] {
use leptos_reactive::*;
use crate::Element;
use futures::{stream::FuturesUnordered, Stream, StreamExt};
/// Renders a component to a static HTML string.
///
/// ```
/// # use leptos_reactive::*; use leptos_dom::*; use leptos_macro::view;
/// let html = render_to_string(|cx| view! { cx,
/// <p>"Hello, world!"</p>
/// });
/// assert_eq!(html, r#"<p data-hk="0-0">Hello, world!</p>"#);
/// ```
pub fn render_to_string(view: impl FnOnce(Scope) -> Element + 'static) -> String {
let runtime = create_runtime();
let html = run_scope(runtime, move |cx| view(cx));
runtime.dispose();
html
}
/// Renders a component to a stream of HTML strings.
///
/// This renders:
@ -30,7 +46,7 @@ cfg_if! {
/// it is waiting for a resource to resolve from the server, it doesn't run it initially.
/// 3) HTML fragments to replace each `<Suspense/>` fallback with its actual data as the resources
/// read under that `<Suspense/>` resolve.
pub fn render_to_stream(view: impl Fn(Scope) -> Element + 'static) -> impl Stream<Item = String> {
pub fn render_to_stream(view: impl FnOnce(Scope) -> Element + 'static) -> impl Stream<Item = String> {
// create the runtime
let runtime = create_runtime();