Skip to content

Commit 4a98551

Browse files
refactor(templates): convert partial templates to macros
Tera works a little differently than Jinja, so it turns out the easiest way to refactor the whole thing so I wasn't including a bunch of if statements everywhere was to switch the partial templates to macros.
1 parent c6be67b commit 4a98551

8 files changed

Lines changed: 82 additions & 69 deletions

File tree

templates/index.html

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
{% extends "base.html" %} {% block content %}
1+
{% extends "base.html" %}
2+
{% import "macros.html" as macros %}
3+
4+
{% block content %}
25
<main>
36
<h2>Recent Posts</h2>
47
<ul>
58
{% for post in posts %}
69
<li>
7-
{% if post.content_type == "Link" %}
8-
{% include "partial/link.html" %}
9-
<p><a href="/post/{{ post.id }}">Permalink</a></p>
10-
{% elif post.content_type == "Post" %}
11-
{% set is_index = true %}
12-
{% include "partial/post.html" %}
13-
{% elif post.content_type == "Quote" %}
14-
{% include "partial/quote.html" %}
15-
<p><a href="/post/{{ post.id }}">Permalink</a></p>
16-
{% endif %}
10+
{{ macros::render_post(post=post, is_index=true, show_permalink=true) }}
1711
</li>
1812
{% endfor %}
1913
</ul>

templates/macros.html

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{% macro post(post, is_index=false) %}
2+
<article>
3+
{% if is_index %}
4+
<h1><a href="/post/{{ post.id }}">{{ post.title }}</a></h1>
5+
{% else %}
6+
<h1>{{ post.title }}</h1>
7+
{% endif %}
8+
<p><small>Published on {{ post.date }}</small></p>
9+
<div>{{ post.content|safe }}</div>
10+
</article>
11+
{% endmacro post %}
12+
13+
{% macro link(post) %}
14+
<article>
15+
{% if post.title %}
16+
<a href="{{ post.link }}">{{ post.title }}</a>
17+
{% else %}
18+
<a href="{{ post.link }}">{{ post.link }}</a>
19+
{% endif %} {% if post.via %} (<a href="{{ post.via }}">via</a>) {%
20+
endif %}
21+
<p><small>Published on {{ post.date }}</small></p>
22+
<div>{{ post.content|safe }}</div>
23+
</article>
24+
{% endmacro link %}
25+
26+
{% macro quote(post) %}
27+
<article>
28+
<p><small>Published on {{ post.date }}</small></p>
29+
<blockquote>
30+
{{ post.content }}
31+
<footer>{{ post.quote_author|display_some }}</footer>
32+
</blockquote>
33+
</article>
34+
{% endmacro quote %}
35+
36+
{% macro commits(post) %}
37+
<details>
38+
<summary>Changes</summary>
39+
{% if post.real_commits %}
40+
<ul>
41+
{% for commit in post.real_commits %}
42+
<li>
43+
<p><b>{{ commit.subject }}</b></p>
44+
<p><small>{{ commit.id }} {{ commit.date }}</small></p>
45+
{% if commit.body %}
46+
<p>{{ commit.body }}</p>
47+
{% endif %}
48+
</li>
49+
{% endfor %}
50+
</ul>
51+
</details>
52+
{% endif %}
53+
{% endmacro commits %}
54+
55+
{% macro render_post(post, is_index=false, show_permalink=false, show_commits=false) %}
56+
{% if post.content_type == "Post" %}
57+
{{ self::post(post=post, is_index=is_index) }}
58+
{% elif post.content_type == "Link" %}
59+
{{ self::link(post=post) }}
60+
{% if show_permalink %}<p><a href="/post/{{ post.id }}">Permalink</a></p>{% endif %}
61+
{% elif post.content_type == "Quote" %}
62+
{{ self::quote(post=post) }}
63+
{% if show_permalink %}<p><a href="/post/{{ post.id }}">Permalink</a></p>{% endif %}
64+
{% endif %}
65+
66+
{% if show_commits %}
67+
{{ self::commits(post=post) }}
68+
{% endif %}
69+
{% endmacro render_post %}

templates/partial/commits.html

Lines changed: 0 additions & 16 deletions
This file was deleted.

templates/partial/link.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

templates/partial/post.html

Lines changed: 0 additions & 9 deletions
This file was deleted.

templates/partial/quote.html

Lines changed: 0 additions & 7 deletions
This file was deleted.

templates/post.html

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
{% extends "base.html" %} {% block content %}
1+
{% extends "base.html" %}
2+
{% import "macros.html" as macros %}
3+
4+
{% block content %}
25
<main>
3-
{% if post.content_type == "Post" %}
4-
{% include "partial/post.html" %}
5-
{% elif post.content_type == "Link" %}
6-
{% include "partial/link.html" %}
7-
{% elif post.content_type == "Quote" %}
8-
{% include "partial/quote.html" %}
9-
{% endif %}
6+
{{ macros::render_post(post=post, show_commits=true) }}
107
</main>
11-
{% include "partial/commits.html" %} {% endblock %}
8+
{% endblock %}

templates/posts.html

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
{% extends "base.html" %}
2+
{% import "macros.html" as macros %}
23

34
{% block content %}
45
<main>
56
<h2>All Posts</h2>
67
<ul>
78
{% for post in posts %}
89
<li>
9-
{% if post.content_type == "Post" %}
10-
{% include "partial/post.html" %}
11-
{% elif post.content_type == "Link" %}
12-
{% include "partial/link.html" %}
13-
{% elif post.content_type == "Quote" %}
14-
{% include "partial/quote.html" %}
15-
{% endif %}
10+
{{ macros::render_post(post=post, is_index=true) }}
1611
</li>
1712
{% endfor %}
1813
</ul>

0 commit comments

Comments
 (0)