# HG changeset patch # User Goffi # Date 1508482179 -7200 # Node ID 07e1543d699225739f932ed636c42c119115801b # Parent f57a8eaec8edcd424a083b00fcbe6f405487be4d template: new "adv_format" filter which use Python's format and return whole value if template is None diff -r f57a8eaec8ed -r 07e1543d6992 src/tools/common/template.py --- 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