On Wed, 26 Apr 2023 at 16:08, Mitch Berkson <[email protected]> wrote:
>
> 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>
>
>
> On Tuesday, April 25, 2023 at 8:39:57 PM UTC-4 Mitch Berkson wrote:
>>
>> 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)?

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

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nikola-discuss/CAMw%2Bj7JoA-QJBrxoLKKqFLEdmAAYZW_KazcRQdhBAKW3LzK2bw%40mail.gmail.com.

Reply via email to