From: Christian Heller Date: Sun, 24 Nov 2024 15:29:27 +0000 (+0100) Subject: Improve templating modularity. X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7Bdb.prefix%7D%7D/%7B%7Bprefix%7D%7D/new_day?a=commitdiff_plain;h=4b2c9cfa23d043353bedb3f9673627ef4e50bb3f;p=ytplom Improve templating modularity. --- diff --git a/templates/_base.tmpl b/templates/_base.tmpl new file mode 100644 index 0000000..8839067 --- /dev/null +++ b/templates/_base.tmpl @@ -0,0 +1,20 @@ +{% import '_macros.tmpl' as macros %} + + + + + + + + +{% block body %} +{% endblock %} + + diff --git a/templates/_macros.tmpl b/templates/_macros.tmpl new file mode 100644 index 0000000..12b28df --- /dev/null +++ b/templates/_macros.tmpl @@ -0,0 +1,13 @@ +{% macro _link_if(cond, target) %}{% if cond %}{% endif %}{{target}}{% if cond %}{% endif %}{% endmacro %} + + +{% macro nav_head(selected="") %} +

+{{ _link_if("playlist" != selected, "playlist") }} +· +{{ _link_if("videos" != selected, "videos") }} +· +{{ _link_if("queries" != selected, "queries") }} +

+
+{% endmacro %} diff --git a/templates/playlist.tmpl b/templates/playlist.tmpl index 70aaa43..58c2b5b 100644 --- a/templates/playlist.tmpl +++ b/templates/playlist.tmpl @@ -1,7 +1,7 @@ - - - - - - - -

playlist · videos · queries

+{% endblock %} + + +{% block body %} +{{ macros.nav_head("playlist") }} {% endfor %}
{% if running %}{% if pause %}PAUSED{% else %}PLAYING{% endif %}{% else %}STOPPED{% endif %}:
@@ -55,6 +57,5 @@ td.history { width: 50%; }
- - +{% endblock %} diff --git a/templates/queries.tmpl b/templates/queries.tmpl index 6ba91cb..457f80e 100644 --- a/templates/queries.tmpl +++ b/templates/queries.tmpl @@ -1,7 +1,8 @@ - - - -

playlist · videos · queries

+{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head("queries") }}

quota: {{quota_count}}/100000

@@ -18,5 +19,4 @@ {% endfor %} - - +{% endblock %} diff --git a/templates/results.tmpl b/templates/results.tmpl index db9d9a5..131d14e 100644 --- a/templates/results.tmpl +++ b/templates/results.tmpl @@ -1,7 +1,8 @@ - - - -

playlist · videos · queries

+{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head() }}

query: {{query}}

{% for video in videos %} @@ -19,5 +20,4 @@ {% endfor %}
- - +{% endblock %} diff --git a/templates/video.tmpl b/templates/video.tmpl index bcdaabb..54d005f 100644 --- a/templates/video.tmpl +++ b/templates/video.tmpl @@ -1,7 +1,8 @@ - - - -

playlist · videos · queries

+{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head() }} @@ -13,7 +14,4 @@ {% endfor %} - - - - +{% endblock %} diff --git a/templates/videos.tmpl b/templates/videos.tmpl index 14c3a2f..5cbf47f 100644 --- a/templates/videos.tmpl +++ b/templates/videos.tmpl @@ -1,12 +1,12 @@ - - - -

playlist · videos · queries

+{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head("videos") }}

downloaded videos:

- - +{% endblock %} diff --git a/templates/yt_video.tmpl b/templates/yt_video.tmpl index 358cced..1ecb258 100644 --- a/templates/yt_video.tmpl +++ b/templates/yt_video.tmpl @@ -1,7 +1,8 @@ - - - -

playlist · videos · queries

+{% extends '_base.tmpl' %} + + +{% block body %} +{{ macros.nav_head() }}
path:{{file.rel_path}}
YouTube ID:{{file.yt_id}}
@@ -19,6 +20,4 @@
title:{{video_data.title}}
thumbnail:
- - - +{% endblock %} diff --git a/ytplom/misc.py b/ytplom/misc.py index 5d7a2f0..809787a 100644 --- a/ytplom/misc.py +++ b/ytplom/misc.py @@ -16,7 +16,7 @@ from urllib.parse import urlparse, parse_qs from urllib.request import urlretrieve from urllib.error import HTTPError # non-included libs -from jinja2 import Template +from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader from mpv import MPV # type: ignore from yt_dlp import YoutubeDL # type: ignore import googleapiclient.discovery # type: ignore @@ -594,6 +594,7 @@ class Server(HTTPServer): def __init__(self, downloads_db: DownloadsDb, *args, **kwargs) -> None: super().__init__(*args, **kwargs) + self.jinja = JinjaEnv(loader=JinjaFSLoader(PATH_TEMPLATES)) self.player = Player() self.downloads = downloads_db @@ -743,10 +744,7 @@ class TaskHandler(BaseHTTPRequestHandler): tmpl_name: PathStr, tmpl_ctx: TemplateContext ) -> None: - with open(path_join(PATH_TEMPLATES, tmpl_name), - 'r', encoding='utf8' - ) as templ_file: - tmpl = Template(str(templ_file.read())) + tmpl = self.server.jinja.get_template(tmpl_name) html = tmpl.render(**tmpl_ctx) self._send_http(bytes(html, 'utf8'))