From 5c5e3b0e4cb646bb26f4d73d33e0795ec338aa76 Mon Sep 17 00:00:00 2001 From: Alphonse Paix Date: Tue, 23 Sep 2025 16:09:25 +0200 Subject: [PATCH] post card fragment --- src/domain/post.rs | 6 +++++ src/templates.rs | 6 +++++ templates/post_card_fragment.html | 41 ++++++++++++++++++++++++++++ templates/posts.html | 44 +------------------------------ 4 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 templates/post_card_fragment.html 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 %} - - {% endfor %} + {% for post in posts %}{{ post.render() | safe }}{% endfor %}