瀏覽代碼

Implement {{ date }} variable for entries

master
Dylan Baker 6 年之前
父節點
當前提交
117c7fff9d
共有 1 個文件被更改,包括 22 次插入0 次删除
  1. 22
    0
      src/entry.rs

+ 22
- 0
src/entry.rs 查看文件

24
 
24
 
25
 impl Entry {
25
 impl Entry {
26
     fn render(&self, template: &str, config: &Config) -> String {
26
     fn render(&self, template: &str, config: &Config) -> String {
27
+        let formatted_date = match self.date {
28
+            Some(date) => date.format("%Y-%m-%d").to_string(),
29
+            None => String::from(""),
30
+        };
27
         template
31
         template
28
             .replace(
32
             .replace(
29
                 "{{ page_title }}",
33
                 "{{ page_title }}",
35
                 "{{ body }}",
39
                 "{{ body }}",
36
                 &markdown_to_html(&self.body, &ComrakOptions::default()),
40
                 &markdown_to_html(&self.body, &ComrakOptions::default()),
37
             )
41
             )
42
+            .replace("{{ date }}", &formatted_date)
38
     }
43
     }
39
 }
44
 }
40
 
45
 
377
         assert_eq!(post.body, "This is the body");
382
         assert_eq!(post.body, "This is the body");
378
         assert_eq!(post.date, None);
383
         assert_eq!(post.date, None);
379
     }
384
     }
385
+
386
+    #[test]
387
+    fn test_display_entry_date() {
388
+        let post = Entry {
389
+            kind: EntryKind::Post,
390
+            title: String::from("Test Title"),
391
+            slug: String::from("one"),
392
+            body: String::from("This is the body"),
393
+            date: Some(NaiveDate::from_ymd(2000, 1, 1)),
394
+        };
395
+
396
+        let config = Config {
397
+            site_name: String::from("Test Site"),
398
+        };
399
+
400
+        assert_eq!("2000-01-01", post.render("{{ date }}", &config));
401
+    }
380
 }
402
 }

Loading…
取消
儲存