Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I include all files from inside a directory in jinja2?

Tags:

jinja2

A normal jinja2 include looks like {% include 'directory/filename.html' %} but I am looking to do something like `{% include 'dropins/*.html' % where obviously the order would be alphabetically.

Is this possible? How?

like image 601
sorin Avatar asked Aug 25 '13 08:08

sorin


People also ask

How do I include in Jinja?

Use the jinja2 {% include %} directive. This will include the content from the correct content-file. Show activity on this post. You can use the include statement.

What is context in Jinja2?

Context contains the dynamic content that you want to inject in your template when it is being rendered.


Video Answer


1 Answers

You could pass a list of file names, then iterate over them:

{% for file_name in file_list %}
    {% include file_name %}
{% endfor %}

And of course, inside file_list you should have already built your list of file names

file_list = ['dropins/file1.html', 'dropins/file2.html']

In Python, I would write some function that could discover all files inside that directory and then save their filenames to the list... or if you know the list just hard code them like above

like image 164
Quentin Donnellan Avatar answered Sep 19 '22 14:09

Quentin Donnellan