mylloon.fr/src/template.rs

42 lines
961 B
Rust
Raw Normal View History

2023-04-09 19:02:06 +02:00
use ramhorns::{Content, Ramhorns};
2023-04-09 19:26:20 +02:00
#[derive(Clone)]
pub struct Template {
pub directory: String,
2023-04-11 01:08:07 +02:00
pub app_name: String,
}
2023-04-11 10:03:22 +02:00
#[derive(Default)]
pub struct Infos {
2023-04-19 20:07:53 +02:00
/// Title
2023-04-11 10:03:22 +02:00
pub page_title: Option<String>,
2023-04-19 20:07:53 +02:00
/// Description
2023-04-11 10:03:22 +02:00
pub page_desc: Option<String>,
2023-04-19 20:07:53 +02:00
/// Keywords
pub page_kw: Option<String>,
2023-04-11 10:03:22 +02:00
}
2023-04-11 01:08:07 +02:00
#[derive(Content)]
struct Data<T> {
app_name: String,
2023-04-11 10:03:22 +02:00
page_title: Option<String>,
page_desc: Option<String>,
2023-04-19 20:07:53 +02:00
page_kw: Option<String>,
2023-04-11 01:08:07 +02:00
data: T,
2023-04-09 19:26:20 +02:00
}
impl Template {
2023-04-11 10:03:22 +02:00
pub fn render<C: Content>(&self, template: &str, data: C, info: Infos) -> String {
2023-04-09 19:26:20 +02:00
let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap();
let tplt = templates.from_file(template).unwrap();
2023-04-09 19:02:06 +02:00
2023-04-11 01:08:07 +02:00
tplt.render(&Data {
app_name: self.app_name.clone(),
2023-04-11 10:03:22 +02:00
page_title: info.page_title,
page_desc: info.page_desc,
2023-04-19 20:07:53 +02:00
page_kw: info.page_kw,
2023-04-11 01:08:07 +02:00
data,
})
2023-04-09 19:26:20 +02:00
}
2023-02-09 11:19:53 +01:00
}