When using PAGE_INDEX how to modify the listing title?

26 views
Skip to first unread message

Mitch Berkson

unread,
Apr 29, 2023, 6:39:58 PM4/29/23
to nikola-discuss
I am using PAGE_INDEX and it creates a nice list with URLs of the pages in each directory. I would like to specify the title of the list instead of having it be the title of the site which is what is used in list.tmpl.

It seems like this should be doable in metadata in a .md file in each directory. Is there a way to do this?

Roberto Alsina

unread,
Apr 29, 2023, 6:44:30 PM4/29/23
to nikola-...@googlegroups.com
I don't think it's implemented but sounds like a nice feature to have.

On Sat, Apr 29, 2023 at 7:40 PM Mitch Berkson <mitc...@gmail.com> wrote:
I am using PAGE_INDEX and it creates a nice list with URLs of the pages in each directory. I would like to specify the title of the list instead of having it be the title of the site which is what is used in list.tmpl.

It seems like this should be doable in metadata in a .md file in each directory. Is there a way to do this?

--
You received this message because you are subscribed to the Google Groups "nikola-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nikola-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nikola-discuss/9a8ade93-17a2-4348-8203-2073d8f3e4e9n%40googlegroups.com.

Mitch Berkson

unread,
Apr 30, 2023, 1:51:10 AM4/30/23
to nikola-discuss
Here's a way I did it. Probably there is a better way.

Add a page to each directory (I used the file name "list_title.md") with the metadata:
list_title = "Heading for the navigation links for this directory"
do_not_list = "True"

In conf.py, define a context variable with the name that was used for the page file with the metadata:
GLOBAL_CONTEXT = {
    ...
    'LIST_TITLE_FILENAME': 'list_title.md',
}

In base_header.tmpl, add the macros:
{# Return the title to use for the navigation links for the directory in which #}
{# link is found by looking for the metadata "list_title" in LIST_TITLE_FILENAME file #}
{% macro html_navigation_links_title(link) %}
    {% set filename = link.split('/')[-2] + ".md" %}
    {% set file_path = link[:-1] + ".md"%}
    {% if filename == LIST_TITLE_FILENAME %}
        {% set full_file_path = os.path.realpath("pages/" + file_path) %}
        {% set f = open(full_file_path, 'r') %}
        {% set file_contents = f.read() %}
        {% set metadata = toml.loads(file_contents.split('+++')[1]) %}
        <h1>{{ metadata['list_title'] }}</h1>
    {% endif %}
{% endmacro %}

{# Return listing for the link if the value of link's do_not_list metadata field is not True #}
{% macro list_link(link, text) %}
    {% set filename = link.split('/')[-2] + ".md" %}
    {% set file_path = link[:-1] + ".md"%}
    {% set full_file_path = os.path.realpath("pages/" + file_path) %}
    {% set f = open(full_file_path, 'r') %}
    {% set file_contents = f.read() %}
    {% set metadata = toml.loads(file_contents.split('+++')[1]) %}
    {% if metadata['do_not_list'] != "True" %}
        <li><a href="{{ link }}">{{ text|e }}</a></li>
    {% endif %}
{% endmacro %}

In list.tmpl, modify the for loop to use the macros:
        <ul class="postlist">
        {% for text, link, count in items %}
            {{ header.html_navigation_links_title(link) }}
            {{ header.list_link(link, text) }}
            {% if count %}
                ({{ count }})
            {% endif %}
        {% endfor %}
        </ul>

Chris Warrick

unread,
Apr 30, 2023, 5:40:47 AM4/30/23
to nikola-...@googlegroups.com
1. You should be able to just set the `title` variable.
2. Reading the files manually seems like an ugly hack. The list.tmpl
template does not get the posts directly (although changing that
wouldn't hurt). But without the posts variable, you could use some
prefix for titles and then filter them out by that prefix. Something
like:

{% for text, link, count in items %}
{% if text.startswith("DIRECTORY-TITLE:") %}
{% set title = text.split("DIRECTORY-TITLE:")[1] %}
{% endif %}
{% endfor %}

And then do the same check for display (but negated). I'm not sure if
this is fully doable from within Jinja2 templates; you might need to
expose some small helper functions in GLOBAL_CONTEXT.

--
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16

Mitch Berkson

unread,
Apr 30, 2023, 10:52:26 AM4/30/23
to nikola-discuss
Well that was a lot easier. Thanks.

Mitch Berkson

unread,
Apr 30, 2023, 6:18:39 PM4/30/23
to nikola-discuss
When using this, I'd like the home page to be displayed directly instead of needing to click the first link. I'm imagining a condition in list.tmpl which would put the html from a home.md file into the content block instead of a link to it. But maybe that can't be done using PAGE_INDEX?

Mitch Berkson

unread,
May 1, 2023, 12:53:25 AM5/1/23
to nikola-discuss
So what I'd like is for the initial page to be served from /output/home/index.html instead of /output/index.html.

Chris Warrick

unread,
May 1, 2023, 4:51:53 AM5/1/23
to nikola-...@googlegroups.com
I'm not sure I understand your question.

If you want to display *only* the custom content, you can put your
content in /pages/index.md with the slug set to index. (This might be
broken in versions older than Nikola v8.2.4.)
If you want to display *both* the custom content and the list of
pages, the easiest way to do it would be to put your custom content as
HTML within list.tmpl, and use {% if 'front_page' in pagekind %} to
control if it’s displayed.
If you want visitors going to / to be redirected to /home/, don’t.

Mitch Berkson

unread,
May 1, 2023, 10:45:44 AM5/1/23
to nikola-discuss
Option 1 is good. I initially tried that but had been using v8.2.3 and it failed because of multiple index.html targets.
Reply all
Reply to author
Forward
0 new messages