From 8a71ea34b959aafcea73127cb08ef0db6ee25d0a Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 20 Oct 2017 08:49:39 +0200 Subject: [PATCH] template: new "adv_format" filter which use Python's format and return whole value if template is None --- src/tools/common/template.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/tools/common/template.py b/src/tools/common/template.py index c4b4c1b5..5dd1b37a 100644 --- a/src/tools/common/template.py +++ b/src/tools/common/template.py @@ -200,6 +200,7 @@ class Renderer(object): self.env.filters['cur_gidx'] = self._cur_gidx self.env.filters['date_days'] = self._date_days self.env.filters['xmlui_class'] = self._xmlui_class + self.env.filters['adv_format'] = self._adv_format def installTranslations(self): i18n_dir = os.path.join(self.base_dir, 'i18n') @@ -361,6 +362,21 @@ class Renderer(object): continue return u' '.join(classes) or None + def _adv_format(self, value, template, **kwargs): + """Advancer formatter + + like format() method, but take care or special values like None + @param value(unicode): value to format + @param template(None, unicode): template to use with format() method. + It will be formatted using value=value and **kwargs + None to return value unchanged + @return (unicode): formatted value + """ + if template is None: + return value + # jinja use string when no special char is used, so we have to convert to unicode + return unicode(template).format(value=value, **kwargs) + def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): """render a template