pub trait Render {
// Provided methods
fn render(&self) -> Markup { ... }
fn render_to(&self, buffer: &mut String) { ... }
}
Expand description
Represents a type that can be rendered as HTML.
To implement this for your own type, override either the .render()
or .render_to()
methods; since each is defined in terms of the
other, you only need to implement one of them. See the example below.
§Minimal implementation
An implementation of this trait must override at least one of
.render()
or .render_to()
. Since the default definitions of
these methods call each other, not doing this will result in
infinite recursion.
§Example
use maud::{html, Markup, Render};
/// Provides a shorthand for linking to a CSS stylesheet.
pub struct Stylesheet(&'static str);
impl Render for Stylesheet {
fn render(&self) -> Markup {
html! {
link rel="stylesheet" type="text/css" href=(self.0);
}
}
}
Provided Methods§
Sourcefn render_to(&self, buffer: &mut String)
fn render_to(&self, buffer: &mut String)
Appends a representation of self
to the given buffer.
Its default implementation just calls .render()
, but you may
override it with something more efficient.
Note that no further escaping is performed on data written to
the buffer. If you override this method, you must make sure that
any data written is properly escaped, whether by hand or using
the Escaper
wrapper struct.