diff --git a/src/domain/post.rs b/src/domain/post.rs index 2fbba8f..f2070f3 100644 --- a/src/domain/post.rs +++ b/src/domain/post.rs @@ -1,3 +1,5 @@ +use crate::templates::PostCardTemplate; +use askama::Template; use chrono::{DateTime, Utc}; use uuid::Uuid; @@ -25,4 +27,8 @@ impl PostEntry { Err(e) => anyhow::bail!(e), } } + + pub fn render(&self) -> String { + PostCardTemplate { post: self }.render().unwrap() + } } diff --git a/src/templates.rs b/src/templates.rs index 1930deb..1cc6bf3 100644 --- a/src/templates.rs +++ b/src/templates.rs @@ -135,3 +135,9 @@ You're receiving this because you subscribed to the zero2prod newsletter."#, ) } } + +#[derive(Template)] +#[template(path = "../templates/post_card_fragment.html")] +pub struct PostCardTemplate<'a> { + pub post: &'a PostEntry, +} diff --git a/templates/post_card_fragment.html b/templates/post_card_fragment.html new file mode 100644 index 0000000..72a7e63 --- /dev/null +++ b/templates/post_card_fragment.html @@ -0,0 +1,41 @@ +
+ +
+
+

{{ post.title }}

+
+
+ + + + +
+ +
+ + + + {{ post.author.as_deref().unwrap_or("Unknown") }} +
+
+
+
+ + + +
+
+
+
diff --git a/templates/posts.html b/templates/posts.html index fac102f..b02d782 100644 --- a/templates/posts.html +++ b/templates/posts.html @@ -21,49 +21,7 @@ {% else %}
- {% for post in posts %} -
- -
-
-

{{ post.title }}

-
-
- - - - -
- -
- - - - {{ post.author.as_deref().unwrap_or("Unknown") }} -
-
-
-
- - - -
-
-
-
- {% endfor %} + {% for post in posts %}{{ post.render() | safe }}{% endfor %}