about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2018-05-22T17·31+0200
committerVincent Ambo <github@tazj.in>2018-05-22T18·44+0200
commit2bbcced032937240ac6fb3f1cfe5136d421b3bea (patch)
tree36d5dbe46b483b19eb7401b3fa299dd6133f98cb /src
parent69583b1236fd329cdc223f2d2a52ff313269d1d1 (diff)
refactor(templates): Move post editing template to Askama
Diffstat (limited to 'src')
-rw-r--r--src/main.rs1
-rw-r--r--src/render.rs27
2 files changed, 14 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs
index 3c533515bf..e25c3bef1f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -140,7 +140,6 @@ fn start_renderer() -> Addr<Syn, Renderer> {
     // location-dependent.
     // Drawback is that template changes require recompilation ...
     tera.add_raw_templates(vec![
-        ("post.html", include_str!("../templates/post.html")),
         ("search.html", include_str!("../templates/search.html")),
     ]).expect("Could not compile templates");
 
diff --git a/src/render.rs b/src/render.rs
index cfae2ad817..d0852fa395 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -26,7 +26,7 @@ use errors::*;
 use std::fmt;
 use md5;
 use models::*;
-use tera::{escape_html, Tera};
+use tera::Tera;
 use chrono::prelude::{DateTime, Utc};
 use comrak::{ComrakOptions, markdown_to_html};
 
@@ -40,7 +40,7 @@ impl Actor for Renderer {
 }
 
 /// Represents a data formatted for human consumption
-#[derive(Debug, Serialize)]
+#[derive(Debug)]
 struct FormattedDate(DateTime<Utc>);
 
 impl fmt::Display for FormattedDate {
@@ -55,7 +55,7 @@ pub struct IndexPage {
 }
 message!(IndexPage, Result<String>);
 
-#[derive(Debug, Serialize)]
+#[derive(Debug)]
 struct IndexThread {
     id: i32,
     title: String,
@@ -104,7 +104,7 @@ pub struct ThreadPage {
 message!(ThreadPage, Result<String>);
 
 // "Renderable" structures with data transformations applied.
-#[derive(Debug, Serialize)]
+#[derive(Debug)]
 struct RenderablePost {
     id: i32,
     body: String,
@@ -163,7 +163,7 @@ impl Handler<ThreadPage> for Renderer {
 
 /// The different types of editing modes supported by the editing
 /// template:
-#[derive(Debug, Serialize)]
+#[derive(Debug, PartialEq)]
 pub enum EditingMode {
     NewThread,
     PostReply,
@@ -174,10 +174,11 @@ impl Default for EditingMode {
     fn default() -> EditingMode { EditingMode::NewThread }
 }
 
-/// This struct represents the context submitted to the template used
-/// for rendering the new thread, edit post and reply to thread forms.
-#[derive(Default, Serialize)]
-pub struct FormContext {
+/// This is the template used for rendering the new thread, edit post
+/// and reply to thread forms.
+#[derive(Template, Default)]
+#[template(path = "post.html")]
+pub struct FormTemplate {
     /// Which editing mode is to be used by the template?
     pub mode: EditingMode,
 
@@ -212,13 +213,13 @@ impl Handler<NewThreadPage> for Renderer {
     type Result = Result<String>;
 
     fn handle(&mut self, msg: NewThreadPage, _: &mut Self::Context) -> Self::Result {
-        let ctx = FormContext {
+        let ctx = FormTemplate {
             alerts: msg.alerts,
             title: msg.title,
             post: msg.post,
             ..Default::default()
         };
-        Ok(self.tera.render("post.html", &ctx)?)
+        ctx.render().map_err(|e| e.into())
     }
 }
 
@@ -233,14 +234,14 @@ impl Handler<EditPostPage> for Renderer {
     type Result = Result<String>;
 
     fn handle(&mut self, msg: EditPostPage, _: &mut Self::Context) -> Self::Result {
-        let ctx = FormContext {
+        let ctx = FormTemplate {
             mode: EditingMode::EditPost,
             id: Some(msg.id),
             post: Some(msg.post),
             ..Default::default()
         };
 
-        Ok(self.tera.render("post.html", &ctx)?)
+        ctx.render().map_err(|e| e.into())
     }
 }