Mercurial > libervia-backend
changeset 2384:d14c1a3e3244
template: new "xmlui_class" filter compute class names from name/values of requested fields.
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 16 Oct 2017 07:44:08 +0200 |
parents | a37457da2bb7 |
children | 39d30cf722cb |
files | src/tools/common/template.py |
diffstat | 1 files changed, 28 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/src/tools/common/template.py Mon Oct 16 07:41:36 2017 +0200 +++ b/src/tools/common/template.py Mon Oct 16 07:44:08 2017 +0200 @@ -196,6 +196,7 @@ self.env.filters['next_gidx'] = self._next_gidx self.env.filters['cur_gidx'] = self._cur_gidx self.env.filters['date_days'] = self._date_days + self.env.filters['xmlui_class'] = self._xmlui_class def installTranslations(self): i18n_dir = os.path.join(self.base_dir, 'i18n') @@ -328,6 +329,33 @@ def _date_days(self, timestamp): return int(time.time() - int(timestamp))/(3600*24) + def attr_escape(self, text): + """escape a text to a value usable as an attribute + + remove spaces, and put in lower case + """ + return text.strip().lower().replace(' ', '_') + + def _xmlui_class(self, xmlui_item, fields): + """return classes computed from XMLUI fields name + + will return a string with a series of escaped {name}_{value} separated by spaces. + @param xmlui_item(xmlui.XMLUIPanel): XMLUI containing the widgets to use + @param fields(iterable(unicode)): names of the widgets to use + @return (unicode, None): computer string to use as class attribute value + None if no field was specified + """ + classes = [] + for name in fields: + escaped_name = self.attr_escape(name) + try: + for value in xmlui_item.widgets[name].values: + classes.append(escaped_name + '_' + self.attr_escape(value)) + except KeyError: + log.debug(_(u"ignoring field \"{name}\": it doesn't exists").format(name=name)) + continue + return u' '.join(classes) or None + def render(self, template, theme=None, locale=DEFAULT_LOCALE, root_path=u'', css_files=None, css_inline=False, **kwargs): """render a template