changeset 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 f57a8eaec8ed
children b29a53fb270d
files src/tools/common/template.py
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/tools/common/template.py	Fri Oct 20 08:48:41 2017 +0200
+++ b/src/tools/common/template.py	Fri Oct 20 08:49:39 2017 +0200
@@ -200,6 +200,7 @@
         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 @@
                 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