pub trait Render:
Send
+ Sync
+ 'static {
type View<'a>: ?Sized;
// Required methods
fn template(&self, name: &str) -> Option<TemplateId>;
fn render(
&self,
template: TemplateId,
view: &Self::View<'_>,
out: &mut Output<'_>,
) -> Result<(), RenderError>;
// Provided method
fn error_page(
&self,
page: &ErrorPage<'_>,
out: &mut Output<'_>,
) -> Result<bool, RenderError> { ... }
}Expand description
A template engine.
The view type is the engine’s: an engine that reads any view through
web-net’s View trait (an adapter for a dynamic engine) uses
type View = dyn View; an engine that compiles templates against the
application’s own types uses that type, and reads it directly.
use web_net::{Output, Render, RenderError, TemplateId, Value, View};
/// Writes the view's `title` field and nothing else.
struct TitleOnly;
impl Render for TitleOnly {
type View<'a> = dyn View + 'a;
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 Associated Types§
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: &Self::View<'_>,
out: &mut Output<'_>,
) -> Result<(), RenderError>
fn render( &self, template: TemplateId, view: &Self::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.
Provided Methods§
Sourcefn error_page(
&self,
page: &ErrorPage<'_>,
out: &mut Output<'_>,
) -> Result<bool, RenderError>
fn error_page( &self, page: &ErrorPage<'_>, out: &mut Output<'_>, ) -> Result<bool, RenderError>
Writes the page for a refusal or an error, from the facts web-net
shows a visitor. Ok(false) (the default) leaves it to web-net’s
built-in page; so does an error, which is not reported further.
§Errors
A RenderError; web-net then sends its built-in page.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".