Skip to main content

Pelican plugin for using Jinja template code within post content

Project description

Jinja2Content Plugin for Pelican

Build Status PyPI Version

This plugin allows the use of Jinja2 directives inside your Pelican articles and pages.

In this approach, your content is first rendered by the Jinja template engine. The result is then passed to the normal Pelican reader as usual. There are two consequences for usage. First, this means the Pelican context and Jinja variables usually visible to your article or page template are not available at rendering time. Second, it means that if any of your input content could be parsed as Jinja directives, they will be rendered as such. This is unlikely to happen accidentally, but it’s good to be aware of.

All input that needs Pelican variables such as article, category, etc., should be put inside your theme’s templating. As such, the main use of this plugin is to automatically generate parts of your articles or pages.

Markdown, reStructured Text, and HTML input are all supported. Note that by enabling this plugin, all input files of these file types will be pre-processed with the Jinja renderer. It is not currently supported to selectively enable or disable jinja2content for only some of these input sources.

Example

One usage is to embed repetitive HTML in Markdown articles. Since Markdown doesn’t allow customization of layout, if anything more sophisticated than just displaying an image is necessary, one is forced to embed HTML in Markdown articles (i.e. hard-code <div> tags and then select them from the theme’s CSS). However, with jinja2content, one can do the following:

File my-cool-article.md

# My cool title

My cool content.

{% from 'img_desc.html' import img_desc %}
{{ img_desc("/images/my-cool-image.png",
    "This is a cool tooltip",
    "This is a very cool image.") }}

Where file img_desc.html contains:

{% macro img_desc(src, title='', desc='') -%}
<div class="img-desc">
  <p><img src="{{ src }}" title="{{ title }}"></p>
  {% if desc %}
  <p><em>{{ desc }}</em></p>
  {% endif %}
</div>
{%- endmacro %}

The result will be:

# My cool title

My cool content.

<div class="img-desc">
  <p><img src="/images/my-cool-image.png" title="This is a cool tooltip"></p>
  <p><em>This is a very cool image.</em></p>
</div>

After this, the Markdown will be rendered into HTML and only then the theme’s templates will be applied.

In this way, Markdown articles have more control over the content that is passed to the theme’s article.html template, without the need to pollute the Markdown with HTML. Another added benefit is that now img_desc is reusable across articles.

Note that templates rendered with jinja2content can contain Markdown as well as HTML, since they are added before the Markdown content is converted to HTML.

Installation

This plugin can be installed via:

pip install pelican-jinja2content

Configuration

This plugin accepts the setting JINJA2CONTENT_TEMPLATES which should be set to a list of paths relative to PATH (the main content directory, usually "content"). jinja2content will look for templates inside these directories, in order. If they are not found in any, the theme’s templates folder is used.

Extending

This plugin is structured such that it should be quite easy to extend readers for other file types to also render Jinja template logic. It should be sufficient to create a new reader class that inherits from the JinjaContentMixin and then your desired reader class. See class definitions in the source for examples.

Contributing

Contributions are welcome and much appreciated. Every little bit helps. You can contribute by improving the documentation, adding missing features, and fixing bugs. You can also help out by reviewing and commenting on existing issues.

To start contributing to this plugin, review the Contributing to Pelican documentation, beginning with the Contributing Code section.

Acknowledgements

  • Original implementation by @joachimneu and re-worked by @Leonardo.
  • Updated to support reST and HTML input by @micahjsmith.
  • Converted to new plugin format by @justinmayer.
  • Replaces pelican-jinja2content, which had become unmaintained.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pelican-jinja2content-1.0.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pelican_jinja2content-1.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pelican-jinja2content-1.0.1.tar.gz.

File metadata

  • Download URL: pelican-jinja2content-1.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.9 Linux/5.4.0-1031-azure

File hashes

Hashes for pelican-jinja2content-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ffc3da9139d55c3667a908330d9babe2b99808f56c87dcb292081324fe114a4b
MD5 c3d39494d9ffd47b1bc70d94490be67c
BLAKE2b-256 bb1102760aa8df46931babd01214f121e35d120a993cdbf914e227bf26650337

See more details on using hashes here.

File details

Details for the file pelican_jinja2content-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pelican_jinja2content-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aeececa7a250aaa0d45a0887ef418409fbf5c89916387de2163b0f8f3bc4ab77
MD5 82ccab970893f6c684dabc99a6172ce7
BLAKE2b-256 df726753de2c9b4f7166efef899c88b232f904e6000efe667cbc69196b156cde

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page