Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pelican-plugins/jinja-filters

Jinja Filters is a plugin for Pelican, a static site generator written in Python.
https://github.com/pelican-plugins/jinja-filters

jinja2 pelican pelican-plugins

Last synced: 3 months ago
JSON representation

Jinja Filters is a plugin for Pelican, a static site generator written in Python.

Awesome Lists containing this project

README

        

=============
Jinja Filters
=============

|build| |pypi|

.. |build| image:: https://img.shields.io/github/actions/workflow/status/pelican-plugins/jinja-filters/main.yml?branch=main
:target: https://github.com/pelican-plugins/jinja-filters/actions
:alt: Build Status

.. |pypi| image:: https://img.shields.io/pypi/v/pelican-jinja-filters.svg
:target: https://pypi.python.org/pypi/pelican-jinja-filters
:alt: PyPI Version

``Jinja Filters`` is a plugin for `Pelican `_,
a static site generator written in Python.

``Jinja Filters`` provides a selection of functions (called *filters*) for
templates to use when building your website. They are packaged for Pelican, but
may prove useful for other projects that make use of
`Jinja2 `_.

Installation
============

The easiest way to install ``Jinja Filters`` is through the use of Pip. This
will also install the required dependencies (currently ``pelican`` and
``titlecase``) automatically.

.. code-block:: sh

pip install pelican-jinja-filters

As ``Jinja Filters`` is a namespace plugin, assuming you are using Pelican 4.5
(or newer) **and** *only* other namespace plugins, ``Jinja Filters`` will be
automatically be loaded by Pelican. And that's it!

If you are using an older version of Pelican, or non-namespace plugins, you may
need to add ``Jinja Filters`` to your ``pelicanconf.py``:

.. code-block:: python

PLUGINS = [
# others...
"pelican.plugins.jinja_filters",
]

The filters are now available for use in your templates.

``Jinja Filters`` supports Pelican from version 3 on.

Usage
=====

At present, the plugin includes the following filters:

- ``datetime`` |--| allows you to change to format displayed for a datetime
object. Optionally supply a `datetime format string
`_
to get a custom format.
- ``article_date`` |--| a specialized version of ``datetime`` that returns
datetimes as wanted for article dates; specifically
*Friday, November 4, 2020*.
- ``breaking_spaces`` |--| replaces non-breaking spaces (HTML code * *)
with normal spaces.
- ``titlecase`` |--| Titlecases the supplied string.
- ``datetime_from_period`` |--| take the ``period`` provided on period archive
pages, and turn it into a proper datetime.datetime object (likely to feed to
another filter)
- ``merge_date_url`` |--| given a datetime (on the left) and a supplied URL,
"apply" the date to it. Envisioned in particular for ``YEAR_ARCHIVE_URL``,
``MONTH_ARCHIVE_URL``, and ``DAY_ARCHIVE_URL``.

For example, within your theme templates, you might have code like:

.. code-block:: html+jinja


Article Published {{ article.date | article_date }}

gives::

Article Published Friday, November 4, 2020

Or with your own date format:

.. code-block:: html+jinja


Article Published {{ article.date | datetime('%b %d, %Y') }}

gives::

Article Published Nov 04, 2020

Filters can also be chained, or applied in sequence. For example to remove
breaking spaces and then titlecase a category name, you might have code like:

.. code-block:: html+jinja


{{ article.category | breaking_spaces | titlecase }}

On a Monthly Archive page, you might have the following to link "up" to the
Yearly Archive page:

.. code-block:: html+jinja


{{ period | datetime_from_period | datetime('%Y') }}

which might give::