Accessing all files in a directory in a template

40 views
Skip to first unread message

Mitch Berkson

unread,
Apr 25, 2023, 8:39:57 PM4/25/23
to nikola-discuss

I'm converting a web site from Hugo to Nikola. Hugo had some way of accessing all the files in a directory. Then I was able to read their metadata, etc. Is there a way to do this in a Nikola template file (using Jinja2 perhaps)?

Message has been deleted

Mitch Berkson

unread,
Apr 26, 2023, 10:08:21 AM4/26/23
to nikola-discuss
Figured it out.

In conf.py:
    import os
    GLOBAL_CONTEXT = {
    ...
    'os': os,}

In .tmpl file:
    {% set dir_path = 'path/to/directory' %}
    {% set filenames = os.listdir(dir_path) %}
    {% set file_list = [] %}
   
    <ul>
        {% for filename in filenames %}
            {% set file_path = os.path.join(dir_path, filename) %}
       
            {% if filename.endswith(".md") %}
                {% set f = open(file_path, 'r') %}
                {% set file_contents = f.read() %}
                <li><a href="{{ filename }}">{{ filename }}</a></li>
            {% endif %}
        {% endfor %}
    </ul>

Chris Warrick

unread,
Apr 26, 2023, 12:32:29 PM4/26/23
to nikola-...@googlegroups.com
While this code probably works now, it is not a good way to do things.
If you add a new file, the page will not be re-rendered, because
Nikola does not know that your page depends on these files. Manually
doing things with the filesystem is not recommended and not supported.

If you want to get a list of posts, you might want one of those features:

* blog indexes
* archives
* tags and categories
* the "post list" directive/shortcode (placed in a page)

If you want to get a list of pages, you might want:

* page folder indexes (PAGE_INDEX setting)
* the "post list" directive/shortcode (placed in a page)

If you want to get a list of code files, the listings feature might be useful.

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

Mitch Berkson

unread,
Apr 26, 2023, 1:33:40 PM4/26/23
to nikola-discuss
Thanks. That sounds better. Unfortunately, I'm lost looking for more information about incorporating page folder indexes into a template or how I would use listings.

Chris Warrick

unread,
Apr 26, 2023, 2:03:18 PM4/26/23
to nikola-...@googlegroups.com
On Wed, 26 Apr 2023 at 19:33, Mitch Berkson <mitc...@gmail.com> wrote:
>
> Thanks. That sounds better. Unfortunately, I'm lost looking for more information about incorporating page folder indexes into a template or how I would use listings.

Nikola has a different approach, it has standardized types of things
and auto-generates it based on other input, with templates whose names
are defined by Nikola. Tags and categories generate an index of tags
and categories and separate index pages per tag/category (with the
output directory structure configurable). Archives are generated for
every year/month/day (with the lowest level of granularity
configurable).

If you enable the PAGE_INDEX setting, index.html files will be
automatically generated for all pages directories and subdirectories.
It will use the list.tmpl template with the pagekind template variable
containing "page_index" (it's a list).

Listings are for displaying source code. If you put files in the
listings/ directory, they will be turned into formatted HTML, with
indexes added.

If you want more granular control, and if your use-case does not fit
the Nikola standard use-cases, you can use the post-list directive in
a page: https://getnikola.com/handbook.html#post-list - It has
filtering options and it lets you customize the template, so it is
probably as flexible as you need (although I recommend exploring the
standard index pages first).

Mitch Berkson

unread,
Apr 27, 2023, 12:45:33 AM4/27/23
to nikola-discuss
When I include the shortcode {{% post-list stop=5 %}}{{% /post-list %}} in my template, there is a jinja2 error which says that % was unexpected. Is that the right way to use the shortcode?

Mitch Berkson

unread,
Apr 27, 2023, 1:02:16 AM4/27/23
to nikola-discuss
I guess I'm not clear whether it is possible to do what I want within Nikola. Maybe I'm thinking about this the wrong way.

I am only interested in pages and not posts or other things. I want to have directories which contain pages (.md files with metadata). For each directory there will be a page which displays links to all the pages in that directory. When I add a page, the directory page is updated when I build the web site. Am I trying to do something which is not well-suited to the Nikola way of doing things?

Chris Warrick

unread,
Apr 28, 2023, 2:34:45 PM4/28/23
to nikola-...@googlegroups.com
Nikola can do that quite easily. What you want to do can be achieved
by the built-in PAGE_INDEX feature. Try setting it to True in conf.py
and see how it looks. As mentioned before, you can change list.tmpl if
you want it to look differently.

Your shortcode didn’t work, because you placed it in a template.
Shortcodes are used in content (posts/pages).
> --
> 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/72b89949-54a8-4c98-a2ad-05960db475c4n%40googlegroups.com.

Mitch Berkson

unread,
Apr 28, 2023, 3:57:07 PM4/28/23
to nikola-discuss
When I set PAGE_INDEX and build, an error says: "Two different tasks can't have a common target.'output\index.html' is a target for render_taxonomies:output\index.html and render_pages:output\index.html."

Separately, putting the shortcode in the right place helped. Thanks.

Chris Warrick

unread,
Apr 28, 2023, 4:53:03 PM4/28/23
to nikola-...@googlegroups.com
You have a page that outputs as index.html, causing a conflict. Change
that page's slug (or remove it) and try again.
> To view this discussion on the web visit https://groups.google.com/d/msgid/nikola-discuss/e9c210c7-e4a7-43f5-bc63-7ae08b01c299n%40googlegroups.com.

Mitch Berkson

unread,
Apr 28, 2023, 5:22:00 PM4/28/23
to nikola-discuss
Aha. Looks good now. Thanks.
Reply all
Reply to author
Forward
0 new messages