use axum::response::{Html, IntoResponse, Response};
use std::path::Path;
use std::sync::Arc;
use tera::{Context, Tera};
pub struct View {
tera: Arc<Tera>,
}
#[macro_export]
macro_rules! context {
($($key:expr => $val:expr),* $(,)?) => {{
let mut ctx = ::tera::Context::new();
$( ctx.insert($key, &$val); )*
ctx
}};
}
impl View {
pub fn new(templates_dir: impl AsRef<Path>) -> Result<Self, tera::Error> {
let pattern = templates_dir.as_ref().join("**/*");
let tera = Tera::new(pattern.to_str().unwrap_or("templates/**/*"))?;
Ok(Self {
tera: Arc::new(tera),
})
}
pub fn render(&self, template: &str, ctx: &Context) -> Result<String, tera::Error> {
let name = format!("{}.html", template);
self.tera.render(&name, ctx)
}
pub fn render_html(&self, template: &str, ctx: &Context) -> Result<Html<String>, tera::Error> {
let html = self.render(template, ctx)?;
Ok(Html(html))
}
pub fn render_response(&self, template: &str, ctx: &Context) -> Result<Response, tera::Error> {
let html = self.render(template, ctx)?;
Ok(Html(html).into_response())
}
pub fn add_raw_template(&mut self, name: &str, raw: &str) -> Result<(), tera::Error> {
Arc::get_mut(&mut self.tera)
.ok_or_else(|| tera::Error::msg("Tera is shared and cannot be mutated"))?
.add_raw_template(name, raw)?;
Ok(())
}
pub fn tera(&self) -> &Tera {
&self.tera
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_macro() {
let ctx = context! {
"foo" => "bar",
"num" => 42,
};
let _ = ctx;
}
#[test]
fn test_create_view_loads_templates() {
let tmp = std::env::temp_dir().join("ravel_view_test");
let _ = std::fs::remove_dir_all(&tmp);
std::fs::create_dir_all(&tmp).unwrap();
std::fs::write(tmp.join("hello.html"), "<h1>Hello, {{ name }}!</h1>").unwrap();
let view = View::new(&tmp).unwrap();
let ctx = context! { "name" => "Ravel" };
let html = view.render("hello", &ctx).unwrap();
assert_eq!(html, "<h1>Hello, Ravel!</h1>");
let _ = std::fs::remove_dir_all(&tmp);
}
}