introducing templating inheritance and macros

This commit is contained in:
mtrx 2024-08-10 21:01:49 +02:00
parent 7fe997acff
commit 2e7a81d7c7
11 changed files with 206 additions and 230 deletions

View file

@ -26,7 +26,8 @@ article {
} }
.post-link { .post-link {
padding-right: 20px; text-decoration: none;
color: black;
} }
.post-title { .post-title {
@ -39,6 +40,12 @@ article {
.post-body { .post-body {
padding-top: 1rem; padding-top: 1rem;
margin-left: 2rem;
}
.submit-box {
display: flex;
justify-content: space-between;
} }
#submit-form { #submit-form {

View file

@ -1,18 +1,12 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en">
<head> {% block head %}
<meta charset="UTF-8"> {{ super() }}
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Submit post</title>
<meta property="og:title" content="{{ username }}' site'" /> <meta property="og:title" content="{{ username }} site" />
<meta property="og:image" content="/static/site-image.png" /> {% endblock head %}
<title>{{ username }}' site</title> {% block content %}
<link rel="stylesheet" href="/static/css/blog.css">
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" />
</head>
<body>
<h1>Hi, I'm {{ username }}</h1> <h1>Hi, I'm {{ username }}</h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/">Back to the blog</a> <a href="/">Back to the blog</a>
@ -29,8 +23,8 @@
<p> <p>
<ul style="list-style: none;"> <ul style="list-style: none;">
{% if github_account %} {% if github_account %}
<li><img class="social-icon" src="/static/social-icons/github.ico"><a <li><img class="social-icon" src="/static/social-icons/github.ico"><a href="https://github.com/{{ github_account }}">
href="https://github.com/{{ github_account }}"> {{ github_account }}</a></li> {{ github_account }}</a></li>
{% endif %} {% endif %}
{% if twitter_account %} {% if twitter_account %}
<li><img class="social-icon" src="/static/social-icons/twitter.ico"><a <li><img class="social-icon" src="/static/social-icons/twitter.ico"><a
@ -50,6 +44,4 @@
{% endif %} {% endif %}
</ul> </ul>
</p> </p>
</body> {% endblock content %}
</html>

View file

@ -0,0 +1,11 @@
{% macro article(id, title, body, publish_date) %}
<article>
<div class="post-content">
<a href="/id/{{ id }}" class="post-link">
<h2 class="post-title">{{ title }}</h2>
</a>
<sub class="post-publish-date"> {{ publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ body }}</p>
</div>
</article>
{% endmacro article %}

View file

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
{% block head %}
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/static/css/blog.css">
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" />
<meta property="og:image" content="/static/site-image.png" />
{% endblock head %}
</head>
<body>
<div id="content">{% block content %}{% endblock content %}</div>
</body>
</html>

View file

@ -1,35 +1,25 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en"> {% import "article.html.macro" as article_macro %}
<head>
<meta charset="UTF-8"> {% block head %}
<meta name="viewport" content="width=device-width, initial-scale=1.0"> {{ super() }}
<meta property="og:title" content="{{ username }}' blog" /> <meta property="og:title" content="{{ username }}' blog" />
<meta property="og:description" content="List of all posts" /> <meta property="og:description" content="List of all posts" />
<meta property="og:image" content="/static/site-image.png" /> <meta property="og:image" content="/static/site-image.png" />
<title> All posts </title> <title> All posts </title>
<link rel="stylesheet" href="/static/css/blog.css"> {% endblock head %}
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico"/>
</head>
<body>
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }}' blog</a></h1> {% block content %}
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/about">About</a> <a href="/about">About</a>
<a href="/">Last 5 posts</a> <a href="/">Last 5 posts</a>
</p> </p>
<ul> <ul>
{% for post in posts %} {% for post in posts %}
<article> {{ article_macro::article(id=post.id,title=post.title,body=post.body,publish_date=post.publish_date) }}
<div>
<a href="/id/{{ post.id }}" class="post-link">[link]</a>
</div>
<div class="post-content">
<h2 class="post-title">{{ post.title }}</h2>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
</div>
</article>
{% endfor %} {% endfor %}
</ul> </ul>
</body>
</html> {% endblock content %}

View file

@ -1,19 +1,14 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en">
<head> {% block head %}
<meta charset="UTF-8"> {{ super() }}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content=" {{ post.title }} | {{ username }}' blog" /> <meta property="og:title" content=" {{ post.title }} | {{ username }}' blog" />
<meta property="og:description" content="{{ post.body | striptags }}" /> <meta property="og:description" content="{{ post.body | striptags }}" />
<meta property="og:image" content="/static/site-image.png" />
<title> {{ post.title }} | {{ username }}' blog </title> <title> {{ post.title }} | {{ username }}' blog </title>
<link rel="stylesheet" href="/static/css/blog.css"> {% endblock head %}
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico"/>
</head>
<body> {% block content %}
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }}' blog</a></h1> <h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/">Home</a> <a href="/">Home</a>
<a href="/about">About</a> <a href="/about">About</a>
@ -21,15 +16,13 @@
</p> </p>
<ul> <ul>
<article> <article>
<div>
<a href="/id/{{ post.id }}" class="post-link">[link]</a>
</div>
<div class="post-content"> <div class="post-content">
<a href="/id/{{ post.id }}" class="post-link">
<h2 class="post-title">{{ post.title }}</h2> <h2 class="post-title">{{ post.title }}</h2>
</a>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub> <sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ post.body }}</p> <p class="post-body">{{ post.body }}</p>
</div> </div>
</article> </article>
</ul> </ul>
</body> {% endblock content %}
</html>

View file

@ -1,38 +1,22 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en"> {% import "article.html.macro" as article_macro %}
<head> {% block head %}
<meta charset="UTF-8"> {{ super() }}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="{{ username }}' blog" /> <meta property="og:title" content="{{ username }}' blog" />
<meta property="og:description" content="Last 5 posts" /> <meta property="og:description" content="Last 5 posts" />
<meta property="og:image" content="/static/site-image.png" /> <title> {{ username }} blog </title>
{% endblock head %}
<title> {{ username }}' blog </title> {% block content %}
<link rel="stylesheet" href="/static/css/blog.css"> <h1><a href="/" class="post-link">{{ username }} blog</a></h1>
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" />
</head>
<body>
<h1><a href="/" class="post-link" style="text-decoration:none;color:black;">{{ username }} blog</a></h1>
<p style="text-align: right"> <p style="text-align: right">
<a href="/about">About</a> <a href="/about">About</a>
<a href="/all">All Posts</a> <a href="/all">All Posts</a>
</p> </p>
<ul> <ul>
{% for post in posts %} {% for post in posts %}
<article> {{ article_macro::article(id=post.id,title=post.title,body=post.body,publish_date=post.publish_date) }}
<div>
<a href="/id/{{ post.id }}" class="post-link">[link]</a>
</div>
<div class="post-content">
<h2 class="post-title">{{ post.title }}</h2>
<sub class="post-publish-date"> {{ post.publish_date | date(format="%Y-%m-%d at %H:%M") }}</sub>
<p class="post-body">{{ post.body }}</p>
</div>
</article>
{% endfor %} {% endfor %}
</ul> </ul>
</body> {% endblock content %}
</html>

View file

@ -1,32 +1,23 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="Edit '{{ title }}'" />
<meta property="og:image" content="/static/site-image.png" />
{% block head %}
{{ super() }}
<title>Edit '{{ title }}'</title> <title>Edit '{{ title }}'</title>
<link rel="stylesheet" href="/static/css/blog.css"> <meta property="og:title" content="Edit '{{ title }}'" />
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" /> {% endblock head %}
</head>
<body>
<div id="cookie-block" hidden>
<label for="token">Password</label>
</br>
<input id="token" type="password" name="token">
</div>
{% block content %}
<form id="submit-form" action="/api/blog/posts/edit/{{ id }}" method=POST> <form id="submit-form" action="/api/blog/posts/edit/{{ id }}" method=POST>
<input class="token" type="text" name="token" hidden> <label for="token">Password</label>
<br />
<input id="token" type="password" name="token">
<br />
<label for="title">Title</label> <label for="title">Title</label>
<textarea id="submit-title" type="text" name="title">{{ title }}</textarea> <textarea id="submit-title" type="text" name="title">{{ title }}</textarea>
<br> <br />
<label for="submit-body">Content</label> <label for="submit-body">Content</label>
<textarea id="submit-body" type="text" name="body">{{ body }}</textarea> <textarea id="submit-body" type="text" name="body">{{ body }}</textarea>
<br> <br />
<button id="submit-button" type="submit">Edit post</button> <button id="submit-button" type="submit">Edit post</button>
</form> </form>
@ -38,6 +29,4 @@
<input class="token" type="text" name="token" hidden> <input class="token" type="text" name="token" hidden>
<button type="submit">Delete post</button> <button type="submit">Delete post</button>
</form> </form>
</body> {% endblock content %}
</html>

View file

@ -1,34 +1,26 @@
<!DOCTYPE html> {% extends "base.html" %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta property="og:title" content="Submit post" />
<meta property="og:image" content="/static/site-image.png" />
{% block head %}
{{ super() }}
<title>Submit post</title> <title>Submit post</title>
<link rel="stylesheet" href="/static/css/blog.css"> <meta property="og:title" content="Submit post" />
<link rel="shortcut icon" type="image/jpg" href="/static/favicon.ico" /> {% endblock head %}
</head>
<body>
<div id="cookie-block" hidden>
<label for="token">Password</label>
<br />
<input id="token" type="password" name="token">
</div>
{% block content %}
<form id="submit-form" action="/api/blog/create" method=POST> <form id="submit-form" action="/api/blog/create" method=POST>
<input class="token" type="text" name="token" hidden>
<label for="title">Title</label> <label for="title">Title</label>
<textarea id="submit-title" type="text" name="title">{{ title }}</textarea> <textarea id="submit-title" type="text" name="title">{{ title }}</textarea>
<br> <br />
<label for="submit-body">Content</label> <label for="submit-body">Content</label>
<textarea id="submit-body" type="text" name="body">{{ body }}</textarea> <textarea id="submit-body" type="text" name="body">{{ body }}</textarea>
<br> <br />
<div class="submit-box">
<div>
<label for="token">Password</label>
<input id="token" type="password" name="token">
</div>
<button id="submit-button" type="submit">Submit</button> <button id="submit-button" type="submit">Submit</button>
</div>
</form> </form>
</body> {% endblock content %}
</html>

View file

@ -26,7 +26,7 @@ async fn blog_create_post(form: Form<NewPostForm>) -> impl Responder {
} }
HttpResponse::MovedPermanently() HttpResponse::MovedPermanently()
.insert_header(("LOCATION", "/blog")) .insert_header(("LOCATION", "/"))
.finish() .finish()
} }
@ -46,7 +46,7 @@ async fn blog_edit_post(post_id: web::Path<String>, form: Form<NewPostForm>) ->
} }
return HttpResponse::MovedPermanently() return HttpResponse::MovedPermanently()
.insert_header(("LOCATION", "/blog")) .insert_header(("LOCATION", "/"))
.finish(); .finish();
} }
@ -65,7 +65,7 @@ async fn blog_delete_post(
} }
return HttpResponse::MovedPermanently() return HttpResponse::MovedPermanently()
.insert_header(("LOCATION", "/blog")) .insert_header(("LOCATION", "/"))
.finish(); .finish();
} }
@ -81,7 +81,7 @@ async fn blog_hide_post(post_id: web::Path<String>, form: Form<BlogActionForm>)
} }
return HttpResponse::MovedPermanently() return HttpResponse::MovedPermanently()
.insert_header(("LOCATION", "/blog")) .insert_header(("LOCATION", "/"))
.finish(); .finish();
} }

View file

@ -22,7 +22,7 @@ async fn main() -> std::io::Result<()> {
Tera::new(format!("{}{}", CONFIG.root_path, "/templates/*").as_str()).unwrap(); Tera::new(format!("{}{}", CONFIG.root_path, "/templates/*").as_str()).unwrap();
tera.autoescape_on(vec![".sql"]); 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::new()
.app_data(Data::new(tera)) .app_data(Data::new(tera))