changeset 531:63ae5eae90ca

templating: Add `now` global to Jinja, improve date error message.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 29 Jul 2015 08:58:41 -0700
parents 1f37f66204b8
children c3c1171679de
files piecrust/templating/jinjaengine.py
diffstat 1 files changed, 8 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/templating/jinjaengine.py	Tue Jul 28 23:56:09 2015 -0700
+++ b/piecrust/templating/jinjaengine.py	Wed Jul 29 08:58:41 2015 -0700
@@ -205,6 +205,7 @@
 
         # Now add globals and filters.
         self.globals.update({
+                'now': get_now_date(),
                 'fail': raise_exception,
                 'highlight_css': get_highlight_css})
 
@@ -317,6 +318,10 @@
     return email.utils.formatdate(value, localtime=localtime)
 
 
+def get_now_date():
+    return time.time()
+
+
 def get_date(value, fmt):
     if value == 'now':
         value = time.time()
@@ -328,12 +333,13 @@
         else:
             suggest_message = ("We can't suggest a proper date format "
                                "for you right now, though.")
-        raise Exception("PieCrust 1 date formats won't work in PieCrust 2. "
+        raise Exception("Got incorrect date format: '%s\n"
+                        "PieCrust 1 date formats won't work in PieCrust 2. "
                         "%s\n"
                         "Please check the `strftime` formatting page here: "
                         "https://docs.python.org/3/library/datetime.html"
                         "#strftime-and-strptime-behavior" %
-                        suggest_message)
+                        (fmt, suggest_message))
     return time.strftime(fmt, time.localtime(value))