mylloon.fr/src/template.rs
2024-01-24 12:10:50 +01:00

72 lines
1.8 KiB
Rust

use ramhorns::{Content, Ramhorns};
/// Structure used in the config variable of the app
#[derive(Clone, Debug)]
pub struct Template {
/// Root directory where templates are stored
pub directory: String,
/// App name
pub app_name: String,
/// URL of app
pub url: String,
/// Name of user
pub name: Option<String>,
}
/// Structure used by /routes/*.rs
#[derive(Default, Debug)]
pub struct Infos {
/// Title
pub page_title: Option<String>,
/// Description
pub page_desc: Option<String>,
/// Keywords
pub page_kw: Option<String>,
}
/// 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,
}
/// Final structure given to template
#[derive(Content, Debug)]
struct Data<T> {
/// App name
app_name: String,
/// App URL
url: String,
/// Title of the page
page_title: Option<String>,
/// Description of the page
page_desc: Option<String>,
/// Keywords of the the page
page_kw: Option<String>,
/// Author's name
page_author: Option<String>,
/// Data needed to render the page
data: T,
}
impl Template {
pub fn render<C: Content>(&self, template: &str, data: C, info: Infos) -> String {
let mut templates: Ramhorns = Ramhorns::lazy(&self.directory).unwrap();
let tplt = templates.from_file(template).unwrap();
tplt.render(&Data {
app_name: self.app_name.to_owned(),
url: self.url.to_owned(),
page_title: info.page_title,
page_desc: info.page_desc,
page_kw: info.page_kw,
page_author: self.name.clone(),
data,
})
}
}