Skip to content

Instantly share code, notes, and snippets.

@QQism
Forked from legenderrys/Pelican Jinja Date Extension
Last active January 4, 2016 03:38
Show Gist options
  • Save QQism/8562827 to your computer and use it in GitHub Desktop.
Save QQism/8562827 to your computer and use it in GitHub Desktop.
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 imp
from datetime import datetime
# Load file by absolute path
jinjaext = imp.load_source('jinjaext', './jinjaext.py')
# 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment