Skip to content

Instantly share code, notes, and snippets.

@hyperking
Last active November 8, 2016 16:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hyperking/7376940 to your computer and use it in GitHub Desktop.
Save hyperking/7376940 to your computer and use it in GitHub Desktop.
Pelican Jinja Date extension and conditional for comparing current datetime to object date.
This will format python datetime as a string to be used within jinja conditionals for comparing current date to article date using pelican app.
1. Create a file named jinjaext.py
2. paste the following snippet
def convertdate(datetime, format='%a-%d-%m-%Y'):
return datetime.date().strftime(format)
3. Add the following line to your config file
import jinjaext
from datetime import datetime
# this line will output your current datetime
CURRDATE = datetime.now().strftime('%a-%d-%m-%Y')
#this line adds your new jinja extension filter
JINJA_FILTERS = {
'convertdate': jinjaext.convertdate ,
}
4. Use the following jinja template loop in your theme to compare post date to current date time
<p>
{% if article.date|convertdate < CURRDATE %}
this is an old post
{% elif article.date|convertdate == CURRDATE %}
this was posted today
{% else %}
this is a future post
{% endif %}
</p>
@QQism
Copy link

QQism commented Jan 22, 2014

I got the error when trying to import the jinjaext. We probably should load jinjaext.py using absolute path. In pelicanconf.py, add these lines

import imp

jinjaext = imp.load_source('jinjaext', './jinjaext.py')

JINJA_FILTERS = {
'convertdate': jinjaext.convertdate ,
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment