mylloon.fr/src/template.rs

73 lines
1.8 KiB
Rust
Raw Normal View History

2023-04-09 19:02:06 +02:00
use ramhorns::{Content, Ramhorns};
2023-04-21 19:47:47 +02:00
/// Structure used in the config variable of the app
2023-05-02 13:34:43 +02:00
#[derive(Clone, Debug)]
2023-04-09 19:26:20 +02:00
pub struct Template {
2023-04-21 19:47:47 +02:00
/// Root directory where templates are stored
2023-04-09 19:26:20 +02:00
pub directory: String,
2023-04-21 19:47:47 +02:00
/// App name
2023-04-11 01:08:07 +02:00
pub app_name: String,
2023-10-24 11:50:30 +02:00
/// URL of app
pub url: String,
2023-10-24 12:21:30 +02:00
/// Name of user
pub name: Option<String>,
2023-04-11 01:08:07 +02:00
}
2023-04-21 19:47:47 +02:00
/// Structure used by /routes/*.rs
#[derive(Debug, Default)]
2023-04-11 10:03:22 +02:00
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
}
2024-01-24 12:10:50 +01:00
/// Information on what page the user is currently
#[derive(Content, Debug, Default)]
pub struct NavBar {
pub index: bool,
pub blog: bool,
pub portfolio: bool,
pub contact: bool,
pub contrib: bool,
pub cours: bool,
}
2023-04-21 19:47:47 +02:00
/// Final structure given to template
2023-05-02 13:34:43 +02:00
#[derive(Content, Debug)]
2023-04-11 01:08:07 +02:00
struct Data<T> {
2023-04-21 19:47:47 +02:00
/// App name
2023-04-11 01:08:07 +02:00
app_name: String,
2023-10-24 11:57:50 +02:00
/// App URL
url: String,
2023-04-21 19:47:47 +02:00
/// Title of the page
2023-04-11 10:03:22 +02:00
page_title: Option<String>,
2023-04-21 19:47:47 +02:00
/// Description of the page
2023-04-11 10:03:22 +02:00
page_desc: Option<String>,
2023-04-21 19:47:47 +02:00
/// Keywords of the the page
2023-04-19 20:07:53 +02:00
page_kw: Option<String>,
2023-10-24 12:21:30 +02:00
/// Author's name
page_author: Option<String>,
2023-04-21 19:47:47 +02:00
/// Data needed to render the page
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 {
2023-04-24 12:18:21 +02:00
app_name: self.app_name.to_owned(),
2023-10-24 12:21:30 +02:00
url: self.url.to_owned(),
2023-04-11 10:03:22 +02:00
page_title: info.page_title,
2023-10-24 12:21:30 +02:00
page_desc: info.page_desc,
page_kw: info.page_kw,
page_author: self.name.clone(),
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
}