Pelican: include file’s contents on main page

One of the benefits of using Python as the settings file format is that Pelican can do what you want without having to write a plugin or modify Pelican itself.

Let’s say you have some introductory content stored in intro.rst that you want to be rendered in your index.html template. The following additions to your Pelican settings file will result in a new INTRO variable that contains the rendered contents of the intro.rst file.

from pelican.settings import DEFAULT_CONFIG
from pelican.readers import RstReader

config = DEFAULT_CONFIG.copy()
# If you need to override default settings (e.g., DOCUTILS_SETTINGS / DEFAULT_LANG):
# config["DEFAULT_LANG"] = "de"

# .read() returns (content, metadata). Keep content only; we don’t need the metadata.
# Assign content to an ALL-CAPS variable to access from template:
INTRO, _ = RstReader(config).read("path/to/intro.rst")

With the above settings in place, you should be able to insert the rendered content by placing the {{ INTRO }} variable in the desired place in your index template.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top