diff --git a/src/lib.rs b/src/lib.rs index 372c495..530c2ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod router; pub mod entities; pub mod utilities; +pub mod template; diff --git a/src/template.rs b/src/template.rs new file mode 100644 index 0000000..d79428e --- /dev/null +++ b/src/template.rs @@ -0,0 +1,5 @@ +use maud::Markup; + +pub trait Template { + fn render(&self) -> Markup; +} diff --git a/src/utilities/responses.rs b/src/utilities/responses.rs index ee7837a..227e388 100644 --- a/src/utilities/responses.rs +++ b/src/utilities/responses.rs @@ -2,6 +2,8 @@ use ascii::AsciiString; use maud::Markup; use tiny_http::{Header, Request, Response}; +use crate::template::Template; + /// Returns a 404 response pub fn respond_not_found(request: Request) { let response = Response::from_string("Not Found").with_status_code(404); @@ -9,6 +11,17 @@ pub fn respond_not_found(request: Request) { let _ = request.respond(response); } +/// Renders a template +pub fn respond_template(request: Request, template: impl Template) { + let response = Response::from_string(template.render()) + .with_header(Header { + field: "Content-Type".parse().unwrap(), + value: AsciiString::from_ascii("text/html; charset=utf8").unwrap(), + }); + + let _ = request.respond(response); +} + /// Renders html pub fn respond_html(request: Request, content: Markup) { let response = Response::from_string(content);