Pelican - How to include different secitons in one long static page?

28 views Asked by At

I want to create a long static website with Pelican and I'm not sure how to do it. I've got 3 sections with corresponding content in md files:

  • landing.md
  • about.md
  • contact.md

Each of this section has separate template defined:

  • landing.html
  • about.html
  • contact.html

I define it in the markdown file with Template: <file>. I would like to simply generate one site that will have all these 3 sections/pages one under another. I tried something like this:

index.html

{% extends "base.html" %}
{% block content %}
<p>index.html</p>
{% for article in articles_page.object_list %}
    {% block article %}{% endblock %}
{% endfor %}
<p>end of index.html</p>
{% endblock %}

and in templates:

{% extends "index.html" %}

{% block head %}
  {{ super() }}
{% endblock %}

{% block article %}
<h1 style="color: red">{{article.title}}</h1>
{{article.content}}
{% endblock %}

but I am not allowed to add block article in the template, it results in articles_page undefined error.

Is it at all possible to generate the file like that in Pelican? What would be the best practice to do it? I would like to have a one long static website and be able to maintain the content conveniently, hence I thought about SSG.

1

There are 1 answers

0
Stu On

If you have the content for the pages in a content/pages directory then you can loop through in index.html them using

{% for p in pages %}
    <h1>{{ p.title }}</h1>
    {{ p.content }}
{% endfor %}

The pages variable is available on all templates.

You may want to have the pages in a certain order and so you can either add an attribute, such as "order" to the meta data and then sort by that with

---
Title: Contact
Order: 1
---
# Contact
{% for p in pages|sort(attribute="order") %}
    <h1>{{ p.title }}</h1>
    {{ p.content }}
{% endfor %}

Or another way is to have multiple loops and filter the loop using selectattr, such as

{% for p in pages|selectattr("title", "eq", "Contact") %}
    <h1>{{ p.title }}</h1>
    {{ p.content }}
{% endfor %}

You could add one of these loops to a partial template for each section and include it into your index.html with

{% include "landing.html" %}
{% include "about.html" %}
{% include "contact.html" %}