Skip to main content

Document

Struct Document 

Source
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>

Source

pub const fn new(head: Head, body: Body) -> Self

Starts document composition with application-owned head and body outputs.

Source

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>"#));
Source

pub fn render(self) -> Result<RenderedDocument, ViewError>

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.

Source

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>.

Trait Implementations§

Source§

impl<Head: Clone, Body: Clone> Clone for Document<Head, Body>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Head: Debug, Body: Debug> Debug for Document<Head, Body>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Head: Eq, Body: Eq> Eq for Document<Head, Body>

Source§

impl<Head: PartialEq, Body: PartialEq> PartialEq for Document<Head, Body>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<Head: PartialEq, Body: PartialEq> StructuralPartialEq for Document<Head, Body>

Auto Trait Implementations§

§

impl<Head, Body> Forget for Document<Head, Body>

§

impl<Head, Body> Freeze for Document<Head, Body>
where DocumentParts<Head, Body>: Freeze,

§

impl<Head, Body> Move for Document<Head, Body>

§

impl<Head, Body> RefUnwindSafe for Document<Head, Body>
where DocumentParts<Head, Body>: RefUnwindSafe,

§

impl<Head, Body> Send for Document<Head, Body>
where DocumentParts<Head, Body>: Send,

§

impl<Head, Body> Sync for Document<Head, Body>
where DocumentParts<Head, Body>: Sync,

§

impl<Head, Body> Unpin for Document<Head, Body>
where DocumentParts<Head, Body>: Unpin,

§

impl<Head, Body> UnsafeUnpin for Document<Head, Body>
where DocumentParts<Head, Body>: UnsafeUnpin,

§

impl<Head, Body> UnwindSafe for Document<Head, Body>
where DocumentParts<Head, Body>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.