Reference

pelican-theming(1)

How to create themes for Pelican There is a community-managed repository of Pelican Themes <https://github.com/getpelican/pelican-themes> for people to share and use. Please note that while we do our best to review and merge theme contributions, they are submitted by the Pelican community and thus may have varying levels of support and interoperability. Community themes can also be found on PyPI tagged with "Framework :: Pelican :: Themes <https://pypi.org/search/?q=&o=-created&c=Framework+%3A%3A+Pelican+%3A%3A+Themes>".

NAME

pelican-theming - How to create themes for Pelican

There is a community-managed repository of Pelican Themes <https://github.com/getpelican/pelican-themes> for people to share and use.

Please note that while we do our best to review and merge theme contributions, they are submitted by the Pelican community and thus may have varying levels of support and interoperability.

Community themes can also be found on PyPI tagged with "Framework :: Pelican :: Themes <https://pypi.org/search/?q=&o=-created&c=Framework+%3A%3A+Pelican+%3A%3A+Themes>".

CREATING THEMES

To generate its HTML output, Pelican uses the Jinja <https://palletsprojects.com/p/jinja/> templating engine due to its flexibility and straightforward syntax. If you want to create your own theme, feel free to take inspiration from the "simple" theme <https://github.com/getpelican/pelican/tree/main/pelican/themes/simple/templates>.

To generate your site using a theme you have created (or downloaded manually and then modified), you can specify that theme via the -t flag:

pelican content -s pelicanconf.py -t /projects/your-site/themes/your-theme

If you'd rather not specify the theme on every invocation, you can define THEME in your settings to point to the location of your preferred theme.

Structure

To make your own theme, you must follow the following structure:

├── static
│   ├── css
│   └── images
└── templates
    ├── archives.html         // to display archives
    ├── article.html          // processed for each article
    ├── author.html           // processed for each author
    ├── authors.html          // must list all the authors
    ├── categories.html       // must list all the categories
    ├── category.html         // processed for each category
    ├── index.html            // the index (list all the articles)
    ├── page.html             // processed for each page
    ├── period_archives.html  // to display time-period archives
    ├── tag.html              // processed for each tag
    └── tags.html             // must list all the tags. Can be a tag cloud.

•

static contains all the static assets, which will be copied to the output theme folder. The above filesystem layout includes CSS and image folders, but those are just examples. Put what you need here.

•

templates contains all the templates that will be used to generate the content. The template files listed above are mandatory; you can add your own templates if it helps you keep things organized while creating your theme.

Templates and Variables

The idea is to use a simple syntax that you can embed into your HTML pages. This document describes which templates should exist in a theme, and which variables will be passed to each template at generation time.

All templates will receive the variables defined in your settings file, as long as they are in all-caps. You can access them directly.

Common Variables

All of these settings will be available to all templates.

VariableDescription
output_fileThe name of the file currently being generated. For instance, when Pelican is rendering the home page, output_file will be "index.html".
articlesThe list of articles, ordered descending by date. All the elements are Article objects, so you can access their attributes (e.g. title, summary, author etc.). Sometimes this is shadowed (for instance, in the tags page). You will then find info about it in the all_articles variable.
datesThe same list of articles, but ordered by date, ascending.
hidden_articlesThe list of hidden articles
draftsThe list of draft articles
period_archivesA dictionary containing elements related to time-period archives (if enabled). See the section Listing and Linking to Period Archives for details.
authorsA list of (author, articles) tuples, containing all the authors and corresponding articles (values)
categoriesA list of (category, articles) tuples, containing all the categories and corresponding articles (values)
tagsA list of (tag, articles) tuples, containing all the tags and corresponding articles (values)
pagesThe list of pages
hidden_pagesThe list of hidden pages
draft_pagesThe list of draft pages

Sorting

URL wrappers (currently categories, tags, and authors), have comparison methods that allow them to be easily sorted by name:

{% for tag, articles in tags|sort %}

If you want to sort based on different criteria, Jinja's sort command <https://jinja.palletsprojects.com/en/latest/templates/#sort> has a number of options.

Date Formatting

Pelican formats the date according to your settings and locale (DATE_FORMATS/DEFAULT_DATE_FORMAT) and provides a locale_date attribute. On the other hand, the date attribute will be a datetime <https://docs.python.org/3/library/datetime.html#datetime-objects> object. If you need custom formatting for a date different than your settings, use the Jinja filter strftime that comes with Pelican. Usage is same as Python strftime <https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior> format, but the filter will do the right thing and format your date according to the locale given in your settings:

{{ article.date|strftime('%d %B %Y') }}

Checking Loaded Plugins

Pelican provides a plugin_enabled Jinja test for checking if a certain plugin is enabled. This test accepts a plugin name as a string and will return a Boolean. Namespace plugins can be specified by full name (pelican.plugins.plugin_name) or short name (plugin_name). The following example uses the webassets plugin to minify CSS if the plugin is enabled and otherwise falls back to regular CSS:

{% if "webassets" is plugin_enabled %}
    {% assets filters="cssmin", output="css/style.min.css", "css/style.scss" %}
        <link rel="stylesheet" href="{{SITEURL}}/{{ASSET_URL}}">
    {% endassets %}
{% else %}
    <link rel="stylesheet" href="{{SITEURL}}/css/style.css}">
{% endif %}

index.html

This is the home page or index of your blog, generated at index.html.

If pagination is active, subsequent pages will reside in index{number}.html.

VariableDescription
articles_paginatorA paginator object for the list of articles
articles_pageThe current page of articles
articles_previous_pageThe previous page of articles (None if page does not exist)
articles_next_pageThe next page of articles (None if page does not exist)
dates_paginatorA paginator object for the article list, ordered by date, ascending.
dates_pageThe current page of articles, ordered by date, ascending.
dates_previous_pageThe previous page of articles, ordered by date, ascending (None if page does not exist)
dates_next_pageThe next page of articles, ordered by date, ascending (None if page does not exist)
page_name'index' -- useful for pagination links

author.html

This template will be processed for each of the existing authors, with output generated according to the AUTHOR_SAVE_AS setting (Default: author/{slug}.html). If pagination is active, subsequent pages will by default reside at author/{slug}{number}.html.

VariableDescription
authorThe name of the author being processed
articlesArticles by this author
datesArticles by this author, but ordered by date, ascending
articles_paginatorA paginator object for the list of articles
articles_pageThe current page of articles
articles_previous_pageThe previous page of articles (None if page does not exist)
articles_next_pageThe next page of articles (None if page does not exist)
dates_paginatorA paginator object for the article list, ordered by date, ascending.
dates_pageThe current page of articles, ordered by date, ascending.
dates_previous_pageThe previous page of articles, ordered by date, ascending (None if page does not exist)
dates_next_pageThe next page of articles, ordered by date, ascending (None if page does not exist)
page_nameAUTHOR_URL where everything after {slug} is removed -- useful for pagination links

category.html

This template will be processed for each of the existing categories, with output generated according to the CATEGORY_SAVE_AS setting (Default: category/{slug}.html). If pagination is active, subsequent pages will by default reside at category/{slug}{number}.html.

VariableDescription
categoryThe name of the category being processed
articlesArticles for this category
datesArticles for this category, but ordered by date, ascending
articles_paginatorA paginator object for the list of articles
articles_pageThe current page of articles
articles_previous_pageThe previous page of articles (None if page does not exist)
articles_next_pageThe next page of articles (None if page does not exist)
dates_paginatorA paginator object for the list of articles, ordered by date, ascending
dates_pageThe current page of articles, ordered by date, ascending
dates_previous_pageThe previous page of articles, ordered by date, ascending (None if page does not exist)
dates_next_pageThe next page of articles, ordered by date, ascending (None if page does not exist)
page_nameCATEGORY_URL where everything after {slug} is removed -- useful for pagination links

article.html

This template will be processed for each article, with output generated according to the ARTICLE_SAVE_AS setting (Default: {slug}.html). The following variables are available when rendering.

VariableDescription
articleThe article object to be displayed
categoryThe name of the category for the current article

Any metadata that you put in the header of the article source file will be available as fields on the article object. The field name will be the same as the name of the metadata field, except in all-lowercase characters.

For example, you could add a field called FacebookImage to your article metadata, as shown below:

Title: I love Python more than music
Date: 2013-11-06 10:06
Tags: personal, python
Category: Tech
Slug: python-je-l-aime-a-mourir
Author: Francis Cabrel
FacebookImage: http://franciscabrel.com/images/pythonlove.png

This new metadata will be made available as article.facebookimage in your article.html template. This would allow you, for example, to specify an image for the Facebook open graph tags that will change for each article:

<meta property="og:image" content="{{ article.facebookimage }}"/>

page.html

This template will be processed for each page, with output generated according to the PAGE_SAVE_AS setting (Default: pages/{slug}.html). The following variables are available when rendering.

VariableDescription
pageThe page object to be displayed. You can access its title, slug, and content.

tag.html

This template will be processed for each tag, with output generated according to the TAG_SAVE_AS setting (Default: tag/{slug}.html). If pagination is active, subsequent pages will by default reside at tag/{slug}{number}.html.

VariableDescription
tagThe name of the tag being processed
articlesArticles related to this tag
datesArticles related to this tag, but ordered by date, ascending
articles_paginatorA paginator object for the list of articles
articles_pageThe current page of articles
articles_previous_pageThe previous page of articles (None if page does not exist)
articles_next_pageThe next page of articles (None if page does not exist)
dates_paginatorA paginator object for the list of articles, ordered by date, ascending
dates_pageThe current page of articles, ordered by date, ascending
dates_previous_pageThe previous page of articles, ordered by date, ascending (None if page does not exist)
dates_next_pageThe next page of articles, ordered by date, ascending (None if page does not exist)
page_nameTAG_URL where everything after {slug} is removed -- useful for pagination links

period_archives.html

This template will be processed for each year of your posts if a path for YEAR_ARCHIVE_SAVE_AS is defined, each month if MONTH_ARCHIVE_SAVE_AS is defined, and each day if DAY_ARCHIVE_SAVE_AS is defined.

VariableDescription
periodA tuple of the form (year, month, day) that indicates the current time period. year and day are numbers while month is a string. This tuple only contains year if the time period is a given year. It contains both year and month if the time period is over years and months and so on.
period_numA tuple of the form (year, month, day), as in period, except all values are numbers.

You can see an example of how to use period in the "simple" theme period_archives.html template <https://github.com/getpelican/pelican/blob/main/pelican/themes/simple/templates/period_archives.html>.

Listing and Linking to Period Archives

The period_archives variable can be used to generate a list of links to the set of period archives that Pelican generates. As a common variable, it is available for use in any template, so you can implement such an index in a custom direct template, or in a sidebar visible across different site pages.

period_archives is a dict that may contain year, month, and/or day keys, depending on which *_ARCHIVE_SAVE_AS settings are enabled. The corresponding value is a list of dicts, where each dict in turn represents a time period (ordered according to the NEWEST_FIRST_ARCHIVES setting) with the following keys and values:

KeyValue
periodThe same tuple as described in period_archives.html, e.g. (2023, 'June', 18).
period_numThe same tuple as described in period_archives.html, e.g. (2023, 6, 18).
urlThe URL to the period archive page, e.g. posts/2023/06/18/. This is controlled by the corresponding *_ARCHIVE_URL setting.
save_asThe path to the save location of the period archive page file, e.g. posts/2023/06/18/index.html. This is used internally by Pelican and is usually not relevant to themes.
articlesA list of Article objects that fall under the time period.
datesSame list as articles, but ordered according to the NEWEST_FIRST_ARCHIVES setting.

Here is an example of how period_archives can be used in a template:

<ul>
{% for archive in period_archives.month %}
    <li>
        <a href="{{ SITEURL }}/{{ archive.url }}">
            {{ archive.period | reverse | join(' ') }} ({{ archive.articles|count }})
        </a>
    </li>
{% endfor %}
</ul>

You can change period_archives.month in the for statement to period_archives.year or period_archives.day as appropriate, depending on the time period granularity desired.

Objects

Detail objects attributes that are available and useful in templates. Not all attributes are listed here, this is a selection of attributes considered useful in a template.

Article

The string representation of an Article is the source_path attribute.

AttributeDescription
authorThe Author of this article.
authorsA list of Authors of this article.
categoryThe Category of this article.
contentThe rendered content of the article.
dateDatetime object representing the article date.
date_formatEither default date format or locale date format.
default_templateDefault template name.
in_default_langBoolean representing if the article is written in the default language.
langLanguage of the article.
locale_dateDate formatted by the date_format.
metadataArticle header metadata dict.
save_asLocation to save the article page.
slugPage slug.
source_pathFull system path of the article source file.
relative_source_pathRelative path from PATH <settings.html#PATH> to the article source file.
statusThe article status, can be any of 'published' or 'draft'.
summaryRendered summary content.
tagsList of Tag objects.
templateTemplate name to use for rendering.
titleTitle of the article.
translationsList of translations Article objects.
urlURL to the article page.

Author / Category / Tag

The string representation of those objects is the name attribute.

AttributeDescription
nameName of this object [1].
page_nameAuthor page name.
save_asLocation to save the author page.
slugPage slug.
urlURL to the author page.
[1]

for Author object, coming from :authors: or AUTHOR.

Page

The string representation of a Page is the source_path attribute.

AttributeDescription
authorThe Author of this page.
contentThe rendered content of the page.
dateDatetime object representing the page date.
date_formatEither default date format or locale date format.
default_templateDefault template name.
in_default_langBoolean representing if the article is written in the default language.
langLanguage of the article.
locale_dateDate formatted by the date_format.
metadataPage header metadata dict.
save_asLocation to save the page.
slugPage slug.
source_pathFull system path of the page source file.
relative_source_pathRelative path from PATH <settings.html#PATH> to the page source file.
statusThe page status, can be any of 'published', 'hidden' or 'draft'.
summaryRendered summary content.
tagsList of Tag objects.
templateTemplate name to use for rendering.
titleTitle of the page.
translationsList of translations Article objects.
urlURL to the page.

Feeds

The feed variables changed in 3.0. Each variable now explicitly lists ATOM or RSS in the name. ATOM is still the default. Old themes will need to be updated. Here is a complete list of the feed variables:

AUTHOR_FEED_ATOM
AUTHOR_FEED_RSS
CATEGORY_FEED_ATOM
CATEGORY_FEED_RSS
FEED_ALL_ATOM
FEED_ALL_RSS
FEED_ATOM
FEED_RSS
TAG_FEED_ATOM
TAG_FEED_RSS
TRANSLATION_FEED_ATOM
TRANSLATION_FEED_RSS

Inheritance

Since version 3.0, Pelican supports inheritance from the simple theme, so you can re-use the simple theme templates in your own themes.

If one of the mandatory files in the templates/ directory of your theme is missing, it will be replaced by the matching template from the simple theme. So if the HTML structure of a template in the simple theme is right for you, you don't have to write a new template from scratch.

You can also extend templates from the simple theme in your own themes by using the {% extends %} directive as in the following example:

{% extends "!simple/index.html" %}   <!-- extends the ``index.html`` template from the ``simple`` theme -->
{% extends "index.html" %}   <!-- "regular" extending -->

Example

With this system, it is possible to create a theme with just one file.

main.css

The file is static/css/main.css (the default CSS_FILE):

body {
    font-family : monospace ;
    font-size : 100% ;
    background-color : white ;
    color : #111 ;
    width : 80% ;
    min-width : 400px ;
    min-height : 200px ;
    padding : 1em ;
    margin : 5% 10% ;
    border : thin solid gray ;
    border-radius : 5px ;
    display : block ;
}
a:link    { color : blue ; text-decoration : none ;      }
a:hover   { color : blue ; text-decoration : underline ; }
a:visited { color : blue ;                               }
h1 a { color : inherit !important }
h2 a { color : inherit !important }
h3 a { color : inherit !important }
h4 a { color : inherit !important }
h5 a { color : inherit !important }
h6 a { color : inherit !important }
pre {
    margin : 2em 1em 2em 4em ;
}
#menu li {
    display : inline ;
}
#post-list {
    margin-bottom : 1em ;
    margin-top : 1em ;
}

Custom templates

If you want to customise the templates as well, you can extend them. For example, to add a custom footer, create a templates/base.html:

{% extends "!simple/base.html" %}
{% block footer %}
    <p>Custom footer</p>
{{ super() }}
{% endblock %}

1.

On the first line, we extend the base.html template from the simple theme, so we don't have to rewrite the entire file.

2.

On the third line, we open the footer block which has already been defined in the simple theme.

3.

On the fourth line, we insert the extra footer content.

4.

On the fifth line, the function super() keeps the content previously inserted in the head block.

5.

On the last line, we close the footer block.

This file will be extended by all the other templates, so the custom footer will appear on all pages.

Author

The Pelican contributors

Copyright

2010–2026