diff options
Diffstat (limited to 'src/controller/utils.rs')
-rw-r--r-- | src/controller/utils.rs | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/controller/utils.rs b/src/controller/utils.rs index 1b58c68..340a5c7 100644 --- a/src/controller/utils.rs +++ b/src/controller/utils.rs @@ -5,7 +5,6 @@ use hyper::header::{ }; use hyper::{Response, StatusCode}; use std::collections::HashMap; -use tera::{Context, Tera}; use crate::controller::error; @@ -23,29 +22,45 @@ pub fn with_headers( pub fn template( assets: &HashMap<String, String>, - templates: &Tera, + templates: &minijinja::Environment<'_>, path: &str, - context: Context, + context: minijinja::Value, ) -> Response<Full<Bytes>> { - let mut context = context; - context.insert("assets", assets); + let context = minijinja::context! { ..context, ..minijinja::context! { + assets => assets + }}; - match templates.render(path, &context) { + match render_template(templates, path, context) { Ok(template) => with_headers( Response::new(template.into()), vec![(CONTENT_TYPE, "text/html"), (CACHE_CONTROL, "no-cache")], ), - Err(err) => server_error( - assets, - templates, - &format!("Erreur lors de la préparation de la page : {:?}", err), - ), + Err(err) => { + log::error!("ERROR template rendering {}\n{:?}", path, err); + server_error( + assets, + templates, + &format!( + "Erreur lors de la préparation de la page : {:?}", + err + ), + ) + } } } +fn render_template( + templates: &minijinja::Environment<'_>, + name: &str, + context: minijinja::Value, +) -> Result<String, minijinja::Error> { + let template = templates.get_template(name)?; + template.render(context) +} + fn server_error( assets: &HashMap<String, String>, - templates: &Tera, + templates: &minijinja::Environment<'_>, msg: &str, ) -> Response<Full<Bytes>> { with_headers( |