axum_template/traits.rs
1use axum::response::IntoResponse;
2use serde::Serialize;
3
4/// An abstraction over different templating engines
5///
6/// # Implementing custom engines
7///
8/// ```
9/// # use axum_template::TemplateEngine;
10/// # use serde::Serialize;
11/// # use std::convert::Infallible;
12///
13/// #[derive(Debug)]
14/// pub struct CustomEngine;
15///
16/// impl TemplateEngine for CustomEngine {
17/// type Error = Infallible;
18/// fn render<S: Serialize>(&self, key: &str, data: S) -> Result<String, Self::Error> {
19/// /* Render your template and return the result */
20/// let result = "Hello world".into();
21/// Ok(result)
22/// }
23/// }
24/// ```
25///
26/// > See the full working example [`custom_engine.rs`]
27///
28/// [`custom_engine.rs`]: https://github.com/Altair-Bueno/axum-template/blob/main/examples/custom_engine.rs
29pub trait TemplateEngine {
30 /// Error type returned if the engine is unable to process the data
31 type Error: IntoResponse;
32
33 /// Renders the template defined by the given key using the Serializable data
34 fn render<S: Serialize>(&self, key: &str, data: S) -> Result<String, Self::Error>;
35}