feat: load embedded templates

master
Aravinth Manivannan 2022-02-23 09:55:06 +05:30
parent 551a48d0c5
commit 1f4053f361
Signed by: realaravinth
GPG Key ID: AD9F0F08E855ED88
5 changed files with 16 additions and 24 deletions

View File

@ -32,7 +32,7 @@ pub struct Login {
ctx: RefCell<Context>,
}
pub const LOGIN: &str = "login";
pub const LOGIN: TemplateFile = TemplateFile::new("login", "pages/auth/login.html");
impl CtxError for Login {
fn with_error(&self, e: &ReadableError) -> String {
@ -51,7 +51,7 @@ impl Login {
}
pub fn render(&self) -> String {
TEMPLATES.render(LOGIN, &self.ctx.borrow()).unwrap()
TEMPLATES.render(LOGIN.name, &self.ctx.borrow()).unwrap()
}
pub fn page(s: &Settings) -> String {

View File

@ -17,27 +17,19 @@
use actix_identity::Identity;
use actix_web::*;
pub use super::{context, Footer, PAGES, PAYLOAD_KEY, TEMPLATES};
pub use super::{context, Footer, TemplateFile, PAGES, PAYLOAD_KEY, TEMPLATES};
pub mod login;
pub mod register;
#[cfg(test)]
mod test;
pub const HOME_BASE: &str = "homebase";
pub const AUTH_BASE: TemplateFile = TemplateFile::new("authbase", "pages/auth/base.html");
pub fn register_templates(t: &mut tera::Tera) {
if let Err(e) = t.add_template_files(vec![
("templates/pages/home/login.html", Some(login::LOGIN)),
(
"templates/pages/home/register.html",
Some(register::REGISTER),
),
("templates/pages/home/base.html", Some(HOME_BASE)),
]) {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
};
for template in [AUTH_BASE, login::LOGIN, register::REGISTER].iter() {
template.register(t).expect(template.name);
}
}
pub fn services(cfg: &mut web::ServiceConfig) {

View File

@ -25,7 +25,7 @@ use crate::AppData;
pub use super::*;
pub const REGISTER: &str = "register";
pub const REGISTER: TemplateFile = TemplateFile::new("login", "pages/auth/register.html");
pub struct Register {
ctx: RefCell<Context>,
@ -48,7 +48,7 @@ impl Register {
}
pub fn render(&self) -> String {
TEMPLATES.render(REGISTER, &self.ctx.borrow()).unwrap()
TEMPLATES.render(REGISTER.name, &self.ctx.borrow()).unwrap()
}
pub fn page(s: &Settings) -> String {

View File

@ -26,17 +26,13 @@ use derive_more::Error;
use serde::*;
use crate::errors::ServiceError;
use super::TemplateFile;
pub const ERROR_KEY: &str = "error";
pub const ERROR_PAGE: &str = "error_comp";
pub const ERROR_TEMPLATE: TemplateFile = TemplateFile::new("error_comp", "components/error.html");
pub fn register_templates(t: &mut tera::Tera) {
if let Err(e) =
t.add_template_files(vec![("templates/components/error.html", Some(ERROR_PAGE))])
{
println!("Parsing error(s): {}", e);
::std::process::exit(1);
};
ERROR_TEMPLATE.register(t).expect(ERROR_TEMPLATE.name);
}
/// Render template with error context

View File

@ -136,6 +136,10 @@ mod tests {
BASE,
FOOTER,
PUB_NAV,
auth::AUTH_BASE,
auth::login::LOGIN,
auth::register::REGISTER,
errors::ERROR_TEMPLATE,
]
.iter()
{