From 2e7a81d7c7427833ce8ff6f6730c53752974eba2 Mon Sep 17 00:00:00 2001 From: mtrx Date: Sat, 10 Aug 2024 21:01:49 +0200 Subject: [PATCH] introducing templating inheritance and macros --- site/content/static/css/blog.css | 9 +- site/content/templates/about.html | 96 ++++++++++------------ site/content/templates/article.html.macro | 11 +++ site/content/templates/base.html | 18 ++++ site/content/templates/blog-all-posts.html | 56 ++++++------- site/content/templates/blog-by-id.html | 59 ++++++------- site/content/templates/blog.html | 56 +++++-------- site/content/templates/edit-form.html | 69 +++++++--------- site/content/templates/submit.html | 52 +++++------- site/src/api.rs | 8 +- site/src/main.rs | 2 +- 11 files changed, 206 insertions(+), 230 deletions(-) create mode 100644 site/content/templates/article.html.macro create mode 100644 site/content/templates/base.html diff --git a/site/content/static/css/blog.css b/site/content/static/css/blog.css index d3502ce..eb57754 100644 --- a/site/content/static/css/blog.css +++ b/site/content/static/css/blog.css @@ -26,7 +26,8 @@ article { } .post-link { - padding-right: 20px; + text-decoration: none; + color: black; } .post-title { @@ -39,6 +40,12 @@ article { .post-body { padding-top: 1rem; + margin-left: 2rem; +} + +.submit-box { + display: flex; + justify-content: space-between; } #submit-form { diff --git a/site/content/templates/about.html b/site/content/templates/about.html index 7d4d5cf..f647ce1 100644 --- a/site/content/templates/about.html +++ b/site/content/templates/about.html @@ -1,55 +1,47 @@ - - +{% extends "base.html" %} - - - - - +{% block head %} +{{ super() }} +Submit post + +{% endblock head %} - {{ username }}' site - - - +{% block content %} +

Hi, I'm {{ username }}

+

+ Back to the blog +

+

+ This is my blog. - -

Hi, I'm {{ username }}

-

- Back to the blog -

-

- This is my blog. - - {% if email %} - If you have questions or input for me please send me an E-Mail to {{ email }} - {% endif %} -

-
-

-

-

- - - \ No newline at end of file + {% if email %} + If you have questions or input for me please send me an E-Mail to {{ email }} + {% endif %} +

+
+

+

+

+{% endblock content %} \ No newline at end of file diff --git a/site/content/templates/article.html.macro b/site/content/templates/article.html.macro new file mode 100644 index 0000000..f250781 --- /dev/null +++ b/site/content/templates/article.html.macro @@ -0,0 +1,11 @@ +{% macro article(id, title, body, publish_date) %} +
+
+ +

{{ title }}

+
+ {{ publish_date | date(format="%Y-%m-%d at %H:%M") }} +

{{ body }}

+
+
+{% endmacro article %} \ No newline at end of file diff --git a/site/content/templates/base.html b/site/content/templates/base.html new file mode 100644 index 0000000..3a994db --- /dev/null +++ b/site/content/templates/base.html @@ -0,0 +1,18 @@ + + + + + {% block head %} + + + + + + {% endblock head %} + + + +
{% block content %}{% endblock content %}
+ + + \ No newline at end of file diff --git a/site/content/templates/blog-all-posts.html b/site/content/templates/blog-all-posts.html index 7f9eeaf..4f3c563 100644 --- a/site/content/templates/blog-all-posts.html +++ b/site/content/templates/blog-all-posts.html @@ -1,35 +1,25 @@ - - - - - - - - +{% extends "base.html" %} +{% import "article.html.macro" as article_macro %} - All posts - - - +{% block head %} +{{ super() }} + + + + All posts +{% endblock head %} - -

{{ username }}' blog

-

- About - Last 5 posts -

- - - + +{% block content %} +

{{ username }} blog

+

+ About + Last 5 posts +

+ + +{% endblock content %} \ No newline at end of file diff --git a/site/content/templates/blog-by-id.html b/site/content/templates/blog-by-id.html index 89f4ec4..d755cc7 100644 --- a/site/content/templates/blog-by-id.html +++ b/site/content/templates/blog-by-id.html @@ -1,35 +1,28 @@ - - - - - - - - +{% extends "base.html" %} - {{ post.title }} | {{ username }}' blog - - - +{% block head %} +{{ super() }} + + + {{ post.title }} | {{ username }}' blog +{% endblock head %} - -

{{ username }}' blog

-

- Home - About - All Posts -

- - - +{% block content %} +

{{ username }} blog

+

+ Home + About + All Posts +

+ +{% endblock content %} \ No newline at end of file diff --git a/site/content/templates/blog.html b/site/content/templates/blog.html index 4fd04a0..751b75a 100644 --- a/site/content/templates/blog.html +++ b/site/content/templates/blog.html @@ -1,38 +1,22 @@ - - +{% extends "base.html" %} +{% import "article.html.macro" as article_macro %} - - - - - - +{% block head %} +{{ super() }} + + + {{ username }} blog +{% endblock head %} - {{ username }}' blog - - - - - -

{{ username }} blog

-

- About - All Posts -

- - - - \ No newline at end of file +{% block content %} +

{{ username }} blog

+

+ About + All Posts +

+ +{% endblock content %} \ No newline at end of file diff --git a/site/content/templates/edit-form.html b/site/content/templates/edit-form.html index 34c5c1e..a8160a3 100644 --- a/site/content/templates/edit-form.html +++ b/site/content/templates/edit-form.html @@ -1,43 +1,32 @@ - - +{% extends "base.html" %} - - - - - +{% block head %} +{{ super() }} +Edit '{{ title }}' + +{% endblock head %} - Edit '{{ title }}' - - - +{% block content %} +
+ +
+ +
+ + +
+ + +
+ +
- - - -
- - - -
- - -
- -
- -
- - -
-
- - -
- - - \ No newline at end of file +
+ + +
+
+ + +
+{% endblock content %} \ No newline at end of file diff --git a/site/content/templates/submit.html b/site/content/templates/submit.html index 869350a..599d7da 100644 --- a/site/content/templates/submit.html +++ b/site/content/templates/submit.html @@ -1,34 +1,26 @@ - - +{% extends "base.html" %} - - - - - +{% block head %} +{{ super() }} +Submit post + +{% endblock head %} - Submit post - - - - - - -
- - - -
- - -
+{% block content %} + + + +
+ + +
+
+
+ + +
- - - - \ No newline at end of file +
+ +{% endblock content %} \ No newline at end of file diff --git a/site/src/api.rs b/site/src/api.rs index 73d200b..7e7eab0 100644 --- a/site/src/api.rs +++ b/site/src/api.rs @@ -26,7 +26,7 @@ async fn blog_create_post(form: Form) -> impl Responder { } HttpResponse::MovedPermanently() - .insert_header(("LOCATION", "/blog")) + .insert_header(("LOCATION", "/")) .finish() } @@ -46,7 +46,7 @@ async fn blog_edit_post(post_id: web::Path, form: Form) -> } return HttpResponse::MovedPermanently() - .insert_header(("LOCATION", "/blog")) + .insert_header(("LOCATION", "/")) .finish(); } @@ -65,7 +65,7 @@ async fn blog_delete_post( } return HttpResponse::MovedPermanently() - .insert_header(("LOCATION", "/blog")) + .insert_header(("LOCATION", "/")) .finish(); } @@ -81,7 +81,7 @@ async fn blog_hide_post(post_id: web::Path, form: Form) } return HttpResponse::MovedPermanently() - .insert_header(("LOCATION", "/blog")) + .insert_header(("LOCATION", "/")) .finish(); } diff --git a/site/src/main.rs b/site/src/main.rs index 08b36b8..5be401f 100644 --- a/site/src/main.rs +++ b/site/src/main.rs @@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> { Tera::new(format!("{}{}", CONFIG.root_path, "/templates/*").as_str()).unwrap(); tera.autoescape_on(vec![".sql"]); - env_logger::Builder::from_env(Env::default().default_filter_or("info")); + env_logger::Builder::from_env(Env::default().default_filter_or("debug")); App::new() .app_data(Data::new(tera))