From cfbcb940be70d2b4e8db24298a2d680f65761314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Romanowski?= Date: Sat, 28 Jan 2023 12:28:30 +0100 Subject: [PATCH] Add the ability to include page titles in HTML title element --- README.md | 19 +++++++++++++++++++ config.toml | 11 +++++++++++ content/pages/about.md | 2 +- content/pages/archive.md | 2 +- ...l-theme.md => welcome-terminimal-theme.md} | 2 +- templates/archive.html | 4 ++++ templates/index.html | 1 + templates/macros/title.html | 17 +++++++++++++++++ templates/page.html | 4 ++++ templates/tags/list.html | 4 ++++ templates/tags/single.html | 5 +++++ 11 files changed, 68 insertions(+), 3 deletions(-) rename content/{terminimal-theme.md => welcome-terminimal-theme.md} (97%) create mode 100644 templates/macros/title.html diff --git a/README.md b/README.md index ef8c7ec..552ef43 100644 --- a/README.md +++ b/README.md @@ -322,6 +322,25 @@ favicon = "/favicon.png" favicon_mimetype = "image/png" ``` +### Page titles + +The theme allows you to configure how the page titles (the `` elements) are rendered. + +Use `"combined"` to render titles as `"Page title | Main title"`. + +```toml +# Optional: Set how <title> elements are rendered. +# Values: +# - "main_only" -- only the main title (`config.title`) is rendered. +# - "page_only" -- only the page title (if defined) is rendered, +# falling back to `config.title` if not defined or empty. +# - "combined" -- combine like so: "page_title | main_title", +# or if page_title is not defined or empty, fall back to `main_title` +# +# Note that the main (index) page only has the main title. +page_titles = "combined" +``` + All the configuration options are also described in [`config.toml`](../master/config.toml). diff --git a/config.toml b/config.toml index 7966430..7af0f3d 100644 --- a/config.toml +++ b/config.toml @@ -78,3 +78,14 @@ use_full_hack_font = false # in your site's "static" directory. # favicon = "/favicon.png" # favicon_mimetype = "image/png" + +# Optional: Set how <title> elements are rendered. +# Values: +# - "main_only" -- only the main title (`config.title`) is rendered. +# - "page_only" -- only the page title (if defined) is rendered, +# falling back to `config.title` if not defined or empty. +# - "combined" -- combine like so: "page_title | main_title", +# or if page_title is not defined or empty, fall back to `main_title` +# +# Note that the main (index) page only has the main title. +page_titles = "main_only" diff --git a/content/pages/about.md b/content/pages/about.md index 36d6d90..054e57c 100644 --- a/content/pages/about.md +++ b/content/pages/about.md @@ -1,5 +1,5 @@ +++ -title = "about me" +title = "About Me" path = "about" +++ diff --git a/content/pages/archive.md b/content/pages/archive.md index 683336b..2c032c0 100644 --- a/content/pages/archive.md +++ b/content/pages/archive.md @@ -1,5 +1,5 @@ +++ +title = "Archive" path = "archive" template = "archive.html" -title = "blog archive (all posts)" +++ diff --git a/content/terminimal-theme.md b/content/welcome-terminimal-theme.md similarity index 97% rename from content/terminimal-theme.md rename to content/welcome-terminimal-theme.md index f153002..f228aae 100644 --- a/content/terminimal-theme.md +++ b/content/welcome-terminimal-theme.md @@ -1,5 +1,5 @@ +++ -title = "Terminimal Theme for Zola" +title = "Welcome to Terminimal Theme for Zola" date = 2019-02-04 [taxonomies] diff --git a/templates/archive.html b/templates/archive.html index dca3acc..af9df6e 100644 --- a/templates/archive.html +++ b/templates/archive.html @@ -1,5 +1,9 @@ {% extends "index.html" %} +{%- block title -%} +{{ title_macros::title(page_title=page.title, main_title=config.title) }} +{%- endblock -%} + {% block content %} <div class="post"> <h1 class="post-title">{{ page.title }}</h1> diff --git a/templates/index.html b/templates/index.html index aa3dd8b..42366b5 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,7 @@ {% import "macros/head.html" as head_macros -%} {% import "macros/menu.html" as menu_macros -%} {% import "macros/post.html" as post_macros -%} +{% import "macros/title.html" as title_macros -%} <!DOCTYPE html> <html lang="{%- if config.default_language -%}{{ config.default_language }}{%- else -%}en{%- endif -%}"> diff --git a/templates/macros/title.html b/templates/macros/title.html new file mode 100644 index 0000000..a8575de --- /dev/null +++ b/templates/macros/title.html @@ -0,0 +1,17 @@ +{% macro title(page_title, main_title) %} + {%- if config.extra.page_titles == "combined" -%} + {%- if page_title -%} + {{ page_title }} | {{ main_title }} + {%- else -%} + {{ main_title }} + {%- endif -%} + {%- elif config.extra.page_titles == "page_only" -%} + {%- if page_title -%} + {{ page_title }} + {%- else -%} + {{ main_title }} + {%- endif -%} + {%- else -%} + {{ main_title }} + {%- endif -%} +{% endmacro title %} diff --git a/templates/page.html b/templates/page.html index 9f3125e..f59d965 100644 --- a/templates/page.html +++ b/templates/page.html @@ -1,5 +1,9 @@ {% extends "index.html" %} +{%- block title -%} +{{ title_macros::title(page_title=page.title, main_title=config.title) }} +{%- endblock -%} + {% block content %} <div class="post"> {{ post_macros::header(page=page) }} diff --git a/templates/tags/list.html b/templates/tags/list.html index 0d2e6bf..98cd693 100644 --- a/templates/tags/list.html +++ b/templates/tags/list.html @@ -1,5 +1,9 @@ {% extends "index.html" %} +{%- block title -%} +{{ title_macros::title(page_title="Tags", main_title=config.title) }} +{%- endblock -%} + {% block content %} <div class="post"> <h1 class="post-title">all tags</h1> diff --git a/templates/tags/single.html b/templates/tags/single.html index 7895912..92183b6 100644 --- a/templates/tags/single.html +++ b/templates/tags/single.html @@ -1,5 +1,10 @@ {% extends "index.html" %} +{%- block title -%} +{% set title = "Tag: " ~ term.name %} +{{ title_macros::title(page_title=title, main_title=config.title) }} +{%- endblock -%} + {% block content %} <div class="post"> <h1 class="post-title">