Parcourir la source

Use helper render function

master
Dylan Baker il y a 3 ans
Parent
révision
c94bbe605c
1 fichiers modifiés avec 9 ajouts et 9 suppressions
  1. 9
    9
      src/main.rs

+ 9
- 9
src/main.rs Voir le fichier

81
     Ok(posts)
81
     Ok(posts)
82
 }
82
 }
83
 
83
 
84
+fn render(template: &str, context: &Context) -> Result<Body, tide::Error> {
85
+    let tera = Tera::new("templates/**/*.html")?;
86
+    let html = tera.render(template, &context)?;
87
+    Ok(Body::from_string(html))
88
+}
89
+
84
 #[async_std::main]
90
 #[async_std::main]
85
 async fn main() -> std::io::Result<()> {
91
 async fn main() -> std::io::Result<()> {
86
     dotenv::dotenv().ok();
92
     dotenv::dotenv().ok();
89
 
95
 
90
     app.with(After(|mut res: Response| async {
96
     app.with(After(|mut res: Response| async {
91
         if let Some(err) = res.downcast_error::<async_std::io::Error>() {
97
         if let Some(err) = res.downcast_error::<async_std::io::Error>() {
92
-            let tera = Tera::new("templates/**/*.html")?;
93
             let mut context = Context::new();
98
             let mut context = Context::new();
94
             context.insert("error", &err.to_string());
99
             context.insert("error", &err.to_string());
95
-            let html = tera.render("error.html", &context)?;
96
-            res.set_body(html);
100
+            res.set_body(render("error.html", &context)?);
97
             res.set_status(StatusCode::InternalServerError);
101
             res.set_status(StatusCode::InternalServerError);
98
         }
102
         }
99
 
103
 
116
     ));
120
     ));
117
 
121
 
118
     app.at("/").get(|req: tide::Request<()>| async move {
122
     app.at("/").get(|req: tide::Request<()>| async move {
119
-        let tera = Tera::new("templates/**/*.html")?;
120
         let posts = read_all_posts().await?;
123
         let posts = read_all_posts().await?;
121
         let mut context = Context::new();
124
         let mut context = Context::new();
122
         context.insert("posts", &posts);
125
         context.insert("posts", &posts);
123
         let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
126
         let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
124
         context.insert("logged_in", &logged_in);
127
         context.insert("logged_in", &logged_in);
125
-        let html = tera.render("index.html", &context)?;
126
-        Ok(Body::from_string(html))
128
+        render("index.html", &context)
127
     });
129
     });
128
 
130
 
129
     app.at("/posts")
131
     app.at("/posts")
143
 
145
 
144
     app.at("/login")
146
     app.at("/login")
145
         .get(|mut req: tide::Request<()>| async move {
147
         .get(|mut req: tide::Request<()>| async move {
146
-            let tera = Tera::new("templates/**/*.html")?;
147
             let mut context = Context::new();
148
             let mut context = Context::new();
148
             let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
149
             let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
149
             context.insert("logged_in", &logged_in);
150
             context.insert("logged_in", &logged_in);
154
                 }
155
                 }
155
                 None => {}
156
                 None => {}
156
             }
157
             }
157
-            let html = tera.render("login.html", &context)?;
158
-            Ok(Body::from_string(html))
158
+            render("login.html", &context)
159
         })
159
         })
160
         .post(|mut req: tide::Request<()>| async move {
160
         .post(|mut req: tide::Request<()>| async move {
161
             let logged_in: bool = req.session().get("logged_in").unwrap_or(false);
161
             let logged_in: bool = req.session().get("logged_in").unwrap_or(false);

Chargement…
Annuler
Enregistrer