pub struct Document<Head, Body> { /* private fields */ }Expand description
Application-provided head and body ready for document composition.
Implementations§
Source§impl<Head, Body> Document<Head, Body>
impl<Head, Body> Document<Head, Body>
Sourcepub const fn new(head: Head, body: Body) -> Self
pub const fn new(head: Head, body: Body) -> Self
Starts document composition with application-owned head and body outputs.
Sourcepub fn with_assets(self, assets: &DocumentAssets) -> Self
pub fn with_assets(self, assets: &DocumentAssets) -> Self
Delivers build-generated assets, such as Trunk’s stylesheets, scripts and WASM bootstrap, with the rendered document.
§Examples
use serenade_view::{Document, DocumentAssets};
let assets = DocumentAssets::new(
r#"<link rel="stylesheet" href="/app-1a2b.css">"#,
r#"<script type="module" src="/app-1a2b.js"></script>"#,
);
let rendered = Document::new("<title>Home</title>", "<main>Welcome</main>")
.with_assets(&assets)
.render()?;
assert!(rendered.as_html().contains(r#"href="/app-1a2b.css"></head>"#));
assert!(rendered.as_html().contains(r#"</script></body>"#));Sourcepub fn render(self) -> Result<RenderedDocument, ViewError>where
DefaultDocumentTemplate: DocumentTemplate<Head, Body>,
pub fn render(self) -> Result<RenderedDocument, ViewError>where
DefaultDocumentTemplate: DocumentTemplate<Head, Body>,
Renders the document through the framework’s default template.
String inputs must already be rendered and escaped; they are not sanitized.
View factories require the leptos-ssr feature and a process-wide SSR
executor initialized with initialize_leptos_ssr or by the application.
§Examples
use serenade_view::Document;
let document = Document::new("<title>Home</title>", "<main>Welcome</main>");
let rendered = document.render()?;
assert!(rendered.as_html().contains("<main>Welcome</main>"));§Errors
Returns the default template’s rendering error, or an asset insertion
error as described for Self::render_with.
Sourcepub fn render_with(
self,
template: impl DocumentTemplate<Head, Body>,
) -> Result<RenderedDocument, ViewError>
pub fn render_with( self, template: impl DocumentTemplate<Head, Body>, ) -> Result<RenderedDocument, ViewError>
Renders the document through an application-provided template.
With the leptos-ssr feature, a template can be a function or closure
accepting the head and body DocumentSlots. Both slots and the
template render under the same request-scoped owner. The application
must initialize the SSR executor before rendering view factories.
§Examples
use serenade_view::{Document, DocumentParts, DocumentTemplate, ViewError};
struct FrenchTemplate;
impl DocumentTemplate<&str, &str> for FrenchTemplate {
fn render(self, parts: DocumentParts<&str, &str>) -> Result<String, ViewError> {
Ok(format!(
"<!DOCTYPE html><html lang=\"fr\"><head>{}</head><body>{}</body></html>",
parts.head, parts.body
))
}
}
let document = Document::new("<title>Accueil</title>", "<main>Bienvenue</main>");
let rendered = document.render_with(FrenchTemplate)?;
assert!(rendered.as_html().contains("<html lang=\"fr\">"));§Errors
Returns the template’s rendering error, or an error when assets are
configured and the rendered document lacks </head> or </body>.