Trait IntoHtml
pub trait IntoHtml {
// Required method
fn into_html(self) -> impl IntoHtml;
// Provided methods
fn escape_and_write(self, buf: &mut String)
where Self: Sized { ... }
fn size_hint(&self) -> usize { ... }
fn into_string(self) -> String
where Self: Sized { ... }
}http and html only.Expand description
A type that can be rendered as a fragment of HTML.
This is the central trait of the HTML templating support. Built-in
scalars (e.g. &str, String, bool, integers, floats) all
implement it; new types can implement it either by returning a
composition of other IntoHtml values, or — for “leaf” types —
by overriding IntoHtml::escape_and_write directly.
§Examples
Compose nested HTML elements using macros:
use rama_http::html::*;
struct Article { title: String, content: String, author: String }
impl IntoHtml for Article {
fn into_html(self) -> impl IntoHtml {
article!(
h1!(self.title),
p!(class = "content", self.content),
footer!("Written by ", self.author),
)
}
}For leaf types, return self to terminate the rendering chain
and override IntoHtml::escape_and_write:
use rama_http::html::{IntoHtml, escape_into};
struct TextNode(String);
impl IntoHtml for TextNode {
fn into_html(self) -> impl IntoHtml { self }
fn escape_and_write(self, buf: &mut String) { escape_into(buf, &self.0); }
fn size_hint(&self) -> usize { self.0.len() }
}Required Methods§
Provided Methods§
fn escape_and_write(self, buf: &mut String)where
Self: Sized,
fn escape_and_write(self, buf: &mut String)where
Self: Sized,
Append the rendered (escaped) HTML to buf.
fn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Best-effort estimate of the rendered byte length, used to pre-allocate the output buffer.
fn into_string(self) -> Stringwhere
Self: Sized,
fn into_string(self) -> Stringwhere
Self: Sized,
Render to a freshly allocated String.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.