comparison piecrust/templating/jinjaengine.py @ 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 cf3218766fe2
children 7df0a959791c
comparison
equal deleted inserted replaced
530:1f37f66204b8 531:63ae5eae90ca
203 # All good! Create the Environment. 203 # All good! Create the Environment.
204 super(PieCrustEnvironment, self).__init__(*args, **kwargs) 204 super(PieCrustEnvironment, self).__init__(*args, **kwargs)
205 205
206 # Now add globals and filters. 206 # Now add globals and filters.
207 self.globals.update({ 207 self.globals.update({
208 'now': get_now_date(),
208 'fail': raise_exception, 209 'fail': raise_exception,
209 'highlight_css': get_highlight_css}) 210 'highlight_css': get_highlight_css})
210 211
211 self.filters.update({ 212 self.filters.update({
212 'keys': get_dict_keys, 213 'keys': get_dict_keys,
315 316
316 def get_email_date(value, localtime=False): 317 def get_email_date(value, localtime=False):
317 return email.utils.formatdate(value, localtime=localtime) 318 return email.utils.formatdate(value, localtime=localtime)
318 319
319 320
321 def get_now_date():
322 return time.time()
323
324
320 def get_date(value, fmt): 325 def get_date(value, fmt):
321 if value == 'now': 326 if value == 'now':
322 value = time.time() 327 value = time.time()
323 if '%' not in fmt: 328 if '%' not in fmt:
324 suggest = php_format_to_strftime_format(fmt) 329 suggest = php_format_to_strftime_format(fmt)
326 suggest_message = ("You probably want a format that looks " 331 suggest_message = ("You probably want a format that looks "
327 "like: '%s'." % suggest) 332 "like: '%s'." % suggest)
328 else: 333 else:
329 suggest_message = ("We can't suggest a proper date format " 334 suggest_message = ("We can't suggest a proper date format "
330 "for you right now, though.") 335 "for you right now, though.")
331 raise Exception("PieCrust 1 date formats won't work in PieCrust 2. " 336 raise Exception("Got incorrect date format: '%s\n"
337 "PieCrust 1 date formats won't work in PieCrust 2. "
332 "%s\n" 338 "%s\n"
333 "Please check the `strftime` formatting page here: " 339 "Please check the `strftime` formatting page here: "
334 "https://docs.python.org/3/library/datetime.html" 340 "https://docs.python.org/3/library/datetime.html"
335 "#strftime-and-strptime-behavior" % 341 "#strftime-and-strptime-behavior" %
336 suggest_message) 342 (fmt, suggest_message))
337 return time.strftime(fmt, time.localtime(value)) 343 return time.strftime(fmt, time.localtime(value))
338 344
339 345
340 class PieCrustFormatExtension(Extension): 346 class PieCrustFormatExtension(Extension):
341 tags = set(['pcformat']) 347 tags = set(['pcformat'])