Last active
November 8, 2016 16:49
-
-
Save legenderrys/7376940 to your computer and use it in GitHub Desktop.
Pelican Jinja Date extension and conditional for comparing current datetime to object date.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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