pub trait Render:
Send
+ Sync
+ 'static {
// Required methods
fn template(&self, name: &str) -> Option<TemplateId>;
fn render(
&self,
template: TemplateId,
view: &dyn View,
out: &mut Output<'_>,
) -> Result<(), RenderError>;
}Expand description
A template engine.
use web_net::{Output, Render, RenderError, TemplateId, Value, View};
/// Writes the view's `title` field and nothing else.
struct TitleOnly;
impl Render for TitleOnly {
fn template(&self, name: &str) -> Option<TemplateId> {
(name == "title").then_some(TemplateId(0))
}
fn render(&self, _t: TemplateId, view: &dyn View, out: &mut Output<'_>) -> Result<(), RenderError> {
let key = view.shape().key("title").ok_or(RenderError::new("demo.no_title"))?;
match view.get(key) {
Value::Text(t) => out.write(t.as_bytes()), // a real engine escapes here
_ => Err(RenderError::new("demo.not_text")),
}
}
}Required Methods§
Sourcefn template(&self, name: &str) -> Option<TemplateId>
fn template(&self, name: &str) -> Option<TemplateId>
Resolves a template name. Called at build time for every template a route names, so an unknown name stops the application from starting.
Sourcefn render(
&self,
template: TemplateId,
view: &dyn View,
out: &mut Output<'_>,
) -> Result<(), RenderError>
fn render( &self, template: TemplateId, view: &dyn View, out: &mut Output<'_>, ) -> Result<(), RenderError>
Renders template against view into out.
§Errors
A RenderError; the request is answered with a 500 error page and
the detail goes to the events, never to the visitor.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".