comparison src/tools/common/template.py @ 2266:084a75b8aa7a

tools (common/template): changed blog_date filter to date_days Instead of returning the whole sentence, date_days only return the number of days between now and the given timestamp. This way the template author can choose how to use it, and translation can be done template side.
author Goffi <goffi@goffi.org>
date Sat, 24 Jun 2017 21:47:06 +0200
parents 322694543225
children 2dae79990122
comparison
equal deleted inserted replaced
2265:322694543225 2266:084a75b8aa7a
193 # we want to have access to SàT constants in templates 193 # we want to have access to SàT constants in templates
194 self.env.globals[u'C'] = C 194 self.env.globals[u'C'] = C
195 # custom filters 195 # custom filters
196 self.env.filters['next_gidx'] = self._next_gidx 196 self.env.filters['next_gidx'] = self._next_gidx
197 self.env.filters['cur_gidx'] = self._cur_gidx 197 self.env.filters['cur_gidx'] = self._cur_gidx
198 self.env.filters['blog_date'] = self._blog_date 198 self.env.filters['date_days'] = self._date_days
199 199
200 def installTranslations(self): 200 def installTranslations(self):
201 i18n_dir = os.path.join(self.base_dir, 'i18n') 201 i18n_dir = os.path.join(self.base_dir, 'i18n')
202 self.translations = {} 202 self.translations = {}
203 for lang_dir in os.listdir(i18n_dir): 203 for lang_dir in os.listdir(i18n_dir):
317 @jinja2.contextfilter 317 @jinja2.contextfilter
318 def _cur_gidx(self, ctx, value): 318 def _cur_gidx(self, ctx, value):
319 """Use current current global index as suffix""" 319 """Use current current global index as suffix"""
320 return u"{}_{}".format(value, ctx['gidx'].current()) 320 return u"{}_{}".format(value, ctx['gidx'].current())
321 321
322 def _blog_date(self, timestamp): 322 def _date_days(self, timestamp):
323 # FIXME: Q&D, need to be done properly 323 return int(time.time() - int(timestamp))/(3600*24)
324 return unicode(int(time.time() - int(timestamp))/(3600*24)) + u" days ago"
325 324
326 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): 325 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs):
327 """render a template 326 """render a template
328 327
329 @param template(unicode): template to render (e.g. blog/articles.html) 328 @param template(unicode): template to render (e.g. blog/articles.html)