comparison src/tools/common/template.py @ 2391:07e1543d6992

template: new "adv_format" filter which use Python's format and return whole value if template is None
author Goffi <goffi@goffi.org>
date Fri, 20 Oct 2017 08:49:39 +0200
parents 39d30cf722cb
children 7bfcc431f66d
comparison
equal deleted inserted replaced
2390:f57a8eaec8ed 2391:07e1543d6992
198 # custom filters 198 # custom filters
199 self.env.filters['next_gidx'] = self._next_gidx 199 self.env.filters['next_gidx'] = self._next_gidx
200 self.env.filters['cur_gidx'] = self._cur_gidx 200 self.env.filters['cur_gidx'] = self._cur_gidx
201 self.env.filters['date_days'] = self._date_days 201 self.env.filters['date_days'] = self._date_days
202 self.env.filters['xmlui_class'] = self._xmlui_class 202 self.env.filters['xmlui_class'] = self._xmlui_class
203 self.env.filters['adv_format'] = self._adv_format
203 204
204 def installTranslations(self): 205 def installTranslations(self):
205 i18n_dir = os.path.join(self.base_dir, 'i18n') 206 i18n_dir = os.path.join(self.base_dir, 'i18n')
206 self.translations = {} 207 self.translations = {}
207 for lang_dir in os.listdir(i18n_dir): 208 for lang_dir in os.listdir(i18n_dir):
359 except KeyError: 360 except KeyError:
360 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) 361 log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name))
361 continue 362 continue
362 return u' '.join(classes) or None 363 return u' '.join(classes) or None
363 364
365 def _adv_format(self, value, template, **kwargs):
366 """Advancer formatter
367
368 like format() method, but take care or special values like None
369 @param value(unicode): value to format
370 @param template(None, unicode): template to use with format() method.
371 It will be formatted using value=value and **kwargs
372 None to return value unchanged
373 @return (unicode): formatted value
374 """
375 if template is None:
376 return value
377 # jinja use string when no special char is used, so we have to convert to unicode
378 return unicode(template).format(value=value, **kwargs)
379
364 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): 380 def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs):
365 """render a template 381 """render a template
366 382
367 @param template(unicode): template to render (e.g. blog/articles.html) 383 @param template(unicode): template to render (e.g. blog/articles.html)
368 @param theme(unicode): template theme 384 @param theme(unicode): template theme